typescript tsc Unexpected token { on import
我知道这是一个常见的问题,ts config中的
节点
tsc
tsconfig.json:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | { "compilerOptions": { "lib": [ "es2017", "dom" ], "target":"es6", "module":"commonjs", "moduleResolution":"node", "outDir":"./build", "emitDecoratorMetadata": true, "experimentalDecorators": true, "sourceMap": false, "rootDir":"src" }, "exclude": [ "node_modules", "**/*.spec.ts", "**/*.test.ts"] } |
导入
1 | import {Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn} from"typeorm"; |
构建
it seems like"module":"commonjs" in ts config must help, but it does not.
应该。由于未使用tsconfig,因此无法正常工作。您应该正在运行
1 | tsc -p . |
首先检查tsconfig,必须是以下配置:
1 2 3 | "target":"es6", "module":"commonjs", "moduleResolution":"node", |
之后,检查是否正在启动JAVASCRIPT文件而不是TYPESCRIPT文件
1 2 3 4 5 | //Wrong: node ....../file.ts //Correct: node ....../file.js |
这是路径问题。 cd目录构建,节点server.js