TypeScript "Compile on save" feature not working in Visual Studio 2015

I stumbled upon this problem today: I fixed that by using the new "watch":true compiler option, also available through JSON in most recent TypeScript versions:

{
  "compilerOptions": {
    "watch": true
  }
}

After doing that, I had to solve another issue related to the following error that appeared in the output window:

Object doesn't support property or method 'watchFile'

It turned out that my system was using an outdated version of TypeScript (1.0.x), despite I was sure I had a newer one that came with the Visual Studio 2015 Update 1 (1.7). If you run into this problem, you can easily check your tsc version by typing tsc -v from a command-prompt.

If it says 1.0.x or anything < 1.7, it's probably due to the fact that you have some old references in your PATH environment variable. Be sure you have 1.7 or later installed by checking inside your Microsoft SDKs folder, which is the one used by Visual Studio to install the TypeScript packages as they get updated:

C:\Program Files (x86)\Microsoft SDKs\TypeScript

If not, update accordingly. Open CPanel > System > Advanced > Environment Variables, select System Variables and click on Edit; browse through the list looking for any reference to the TypeScript folder, change one of them to make it point to your TypeScript most recent installed version (1.7 or above) and delete any other dupes. See also screenshot below:

enter image description here

For additional details, read this post on my blog.


Solution:

For me, and I am quite sure this is also the case for others, this was due to a mistake in the tsconfig.json.

You need to add "compileOnSave": true. But in the global section not inside compilerOptions.

Wrong:
{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "compileOnSave": true

  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

Correct:
{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5"

  },
"compileOnSave": true,
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

Best regards,

Anders Both Basechat.


For me it was this option in tsconfig.json:

"compileOnSave": true,
"compilerOptions": { ... },

Restart Visual Studio for this change to take effect.