问题
node: v10.16.3
npm: 6.12.0
I got a error when I import express in node.
I'm using this code https://github.com/angular-university/rxjs-course, look at server/server.ts
.
I run server.ts
with
$ ts-node ./server/server.ts
The related code is:
import * as express from 'express';
The error is:
import * as express from 'express';
^
SyntaxError: Unexpected token *
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:493:23)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Object.require.extensions.(anonymous function) [as .ts] (/usr/local/lib/node_modules/ts-node/src/index.ts:496:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at Object.<anonymous> (/usr/local/lib/node_modules/ts-node/src/bin.ts:158:12)
at Module._compile (internal/modules/cjs/loader.js:778:30)
I already tried to change the code with:
import express from 'express';
回答1:
I've solved using @ktad's suggestion:
I added "target": "es2017",
in server.tsconfig.json
.
Here my full server.tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"lib": ["es2017"]
}
}
来源:https://stackoverflow.com/questions/58463231/node-js-syntaxerror-unexpected-token-when-import-express