Ts-node : SyntaxError: Cannot use import statement outside a module
To run ts-node (or plain node for that matter) you need to use "module": "commonjs", "target": "ES2017"
, otherwise the import
/export
statements are illegally placed in an IIFE.
So I would suggest using another file called node.tsconfig.json with the following contents:
{ "extends": "./tsconfig.json", "compilerOptions": { "target": "ES2017" // For NodeJS 8 compat, see https://www.typescriptlang.org/tsconfig#target for more info }}
And then run ts-node with --project ./node.tsconfig.json
I had the same issue and I fixed it by changing the module
in tsconfig.json
to commonjs
and removing the module
key in package.json
:
// tsconfig.json{ "module": "CommonJS",}