Ts-node: Unable to start ts-node on Windows

Created on 12 Jun 2018  路  9Comments  路  Source: TypeStrong/ts-node

I'm using nodemon to watch for changes in my project.
In the nodemon.json file I have the following configuration:

{
    "watch": [
        "server/**/*.ts"
    ],
    "execMap": {
        "ts": "ts-node --compilerOptions '{\"module\":\"commonjs\"}'"
    }
}

When trying to start the server nodemon server/index.ts , this is the error I get:

> nodemon server/index.ts[nodemon] 1.17.5[nodemon] to restart at any time, enter `rs`[nodemon] watching: server/**/*.ts
[nodemon] starting `ts-node --compilerOptions '{"module":"commonjs"}' server/index.ts`
undefined:1'{module:commonjs}'
^
SyntaxError: Unexpected token ' in JSON at position 0
    at JSON.parse (<anonymous>)    at Object.parse (C:\pp\habit\web\node_modules\ts-node\dist\index.js:83:45)    at Object.<anonymous> (C:\pp\habit\web\node_modules\ts-node\dist\bin.js:64:30)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)    at Function.Module._load (internal/modules/cjs/loader.js:543:3)    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)    at startup (internal/bootstrap/node.js:238:19)
[nodemon] app crashed - waiting for file changes before starting...

It seems the compilerOptions are not being passed to the command on windows. Any idea how to solve this 馃

Most helpful comment

Yes that was the problem thank you 馃憤
For anyone interested this is how you should pass the parameters on windows:

{
    "watch": [
        "server/**/*.ts"
    ],
    "execMap": {
        "ts": "ts-node --compilerOptions {\"\"\"module\"\"\":\"\"\"commonjs\"\"\"}"
    }
}

All 9 comments

It seems they are being passed, otherwise there wouldn鈥檛 be an error. And, if you look at the error you provided, it appears the double quotes are being stripped. I鈥檓 not a Windows expert, but I think you may need to escape your double quotes within the string so they are not removed by the Windows shell. Can you try that?

Yes that was the problem thank you 馃憤
For anyone interested this is how you should pass the parameters on windows:

{
    "watch": [
        "server/**/*.ts"
    ],
    "execMap": {
        "ts": "ts-node --compilerOptions {\"\"\"module\"\"\":\"\"\"commonjs\"\"\"}"
    }
}

Awesome, glad to here. I'm actually not really sure why it's the case. Feel free to comment if you know what's going on or have a link to documentation that could be used in the README for future issues.

This way the script is not running on linux. Is there any way to make it cross-os?

@fasfsfgs , the only way I found to make it work on windows without resorting to that ugly as hell and non-portable triple quote escaping is to use environment variables:
set TS_NODE_COMPILER_OPTIONS={"module":"commonjs"} on windows worked for me and I removed the --compilerOptions from the script invocation.

any update on making this cross OS?

@jamiehaywood it already works on Windows. As described above, you will need to obey the escaping rules for whatever shell you're using, and this rule applies to all CLI tools across OSes, not just ts-node.

What change, exactly, are you proposing?

I managed to make it crossOS with those "ugly as hell" escapes and cross-env module.

cross-env TS_NODE_COMPILER_OPTIONS={\\\"module\\\":\\\"commonjs\\\"} ts-node

Remember you can specify all typescript and ts-node options in your tsconfig file, too.

For example:

{
  "ts-node": {
    "skipIgnore": true, // ts-node options
    "compilerOptions": {/* ts-node overrides if you need ts-node to use different compiler options than tsc.  This is usually not necessary */}
  },
  "compilerOptions": {/* ... */}
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

nehalist picture nehalist  路  3Comments

aj-r picture aj-r  路  3Comments

grissius picture grissius  路  3Comments

mattdell picture mattdell  路  4Comments

motss picture motss  路  4Comments