I dont' know if this is a node.js problem or TypeScript compiler. I just want to compile my code and run it under node.js without any special setup.
tsc -v: Version 2.9.2
node -v: v10.6.0
So i have this in my ts file:
import { assert } from 'chai';
which causes this error in node:
(function (exports, require, module, __filename, __dirname) { import { assert } from 'chai';
^
SyntaxError: Unexpected token {
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Object.runInThisContext (vm.js:298:10)
And here's my tsconfig
file. I'm hoping that something in my config file is causing this:
{
"include" : [
"src",
"test",
"unittest"
],
"compileOnSave": true,
"compilerOptions": {
"module": "es2015",
"moduleResolution": "node",
"esModuleInterop": true,
"target": "es5",
"noImplicitAny": true,
"declaration": true,
"sourceMap": true,
"preserveConstEnums": true,
"lib": [
"es2015", "dom"
],
"noUnusedLocals": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"alwaysStrict": true,
"strictNullChecks": false,
"noUnusedParameters": false,
"pretty": true,
"allowUnreachableCode": false,
"experimentalDecorators": true,
"suppressImplicitAnyIndexErrors": true,
"outDir": "./build"
}
}
If you want to run your code in Node.js, you need to tell typescript to transpile the ES module syntax to CommonJS. In your tsconfig:
"module": "commonjs",
Thank you so much. That helped instantly.
Closing this.
Thanks, it helped
Thank you!! I was really stuck on this, after working on Angular apps for the browser. Immediately fixed my problem.
was wandering if i can get some help with the same problem:
https://stackoverflow.com/questions/56014054/typescript-tsc-unexpected-token-on-import
"module": "commonjs"
doesn't help for some reason.
"module": "commonjs"
, didn't help me.
This is the error I get:
import {Given, Then, When} from "cucumber";
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:721:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
And this is my tsconfig.json file:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"declaration": true,
"sourceMap": true,
"outDir": "built/",
"removeComments": false,
"noImplicitAny": true,
"moduleResolution": "node",
"esModuleInterop": true
},
"exclude": [
"built",
"node_modules",
"testapp",
"website",
"scripts",
"exampleTypescript",
"spec/**/*"
]
}
Same issue.
As @AbrahamNuno said, "module": "commonjs"
doesn't help anymore. Is there any other way this can be fixed?
here is mine just for reference.
changing back to commonjs fixed my issue
{
"compilerOptions": {
"noImplicitAny": false,
"outDir": "dist",
"baseUrl": "src",
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators":true,
"emitDecoratorMetadata": true,
"sourceMap": true,
"declaration": false,
"strictNullChecks": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"allowJs": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"typeRoots": ["./src/types", "./node_modules/@types"],
"resolveJsonModule": true,
"noEmit": true,
"skipLibCheck": true,
"isolatedModules": true,
},
"include": [
"src/**/*"
]
}
Just had the same issue now
Most helpful comment
If you want to run your code in Node.js, you need to tell typescript to transpile the ES module syntax to CommonJS. In your tsconfig: