[英]ts-node: Unexpected token ':'
I am writing a backend server for my app and decided to change babel-node executor to ts-node .我正在为我的应用程序编写后端服务器,并决定将babel-node executor 更改为ts-node 。 My command in package.json is:
我在package.json中的命令是:
"server": "cd server && ts-node --project tsconfig.json -r ts-node/register Server.ts",
But keep getting the same error:但不断收到同样的错误:
> nodejs-server@1.0.0 server D:\Projects\Visual Studio Code\NodeJS Server
> cd server && ts-node --project tsconfig.json -r ts-node/register Server.ts
(node:3824) ExperimentalWarning: The ESM module loader is experimental.
SyntaxError: Unexpected token ':'
at Loader.moduleStrategy (internal/modules/esm/translators.js:83:18)
at link (internal/modules/esm/module_job.js:36:21)
I tried compiling a small file, but compiler does not understand typescript.我尝试编译一个小文件,但编译器不理解打字稿。
test.ts :测试.ts :
var test: string = 'Hello!';
console.log(test);
I get the same error:我犯了同样的错误:
PS D:\Projects\Visual Studio Code\NodeJS Server\server> npx ts-node test.ts
(node:1420) ExperimentalWarning: The ESM module loader is experimental.
SyntaxError: Unexpected token ':'
at Loader.moduleStrategy (internal/modules/esm/translators.js:83:18)
at link (internal/modules/esm/module_job.js:36:21)
I have already reinstalled ts-node and typescript modules.我已经重新安装了ts-node和typescript模块。
Root tsconfig.json :根tsconfig.json :
{
"compilerOptions": {
"target": "ES6",
"moduleResolution": "Node",
"traceResolution": false,
"allowJs": false,
"esModuleInterop": true,
"declaration": false,
"noResolve": false,
"noImplicitAny": false,
"removeComments": true,
"strictNullChecks": false,
"sourceMap": false,
"skipLibCheck": true,
"resolveJsonModule": true,
"typeRoots": [
"custom_typings",
"node_modules/@types",
],
},
"include": [
"client",
"server",
],
"exclude": [
"node_modules",
"client/bundle.js"
]
}
server/tsconfig.json :服务器/tsconfig.json :
{
"extends": "../tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"jsx": "preserve",
"noEmit": true,
"baseUrl": "../",
"paths": {
"*": ["node_modules/*"],
"@server/*": ["server/*"],
"@client/*": ["client/*"],
"@routes/*": ["server/routes/*"],
"@public/*": ["public/*"],
"@secure/*": ["secure/*"],
"@utils/*": ["utils/*"],
},
},
"include": ["**/*"]
}
尝试重命名: Server.js
到Server.ts
Found a solution.找到了解决办法。 In the project configuration (file package.json ) it was necessary to remove the modular type of work JS.
在项目配置(文件package.json )中,有必要删除工作 JS 的模块化类型。
Line: "type": "module",
行:
"type": "module",
If you have a tsconfig, make sure nothing is set to "ESNext".如果您有 tsconfig,请确保未将任何内容设置为“ESNext”。 This could be either the
target
or module
arguments.这可以是
target
或module
参数。
If running it from command line, try this:如果从命令行运行它,试试这个:
"ts-node -O '{\\"module\\": \\"commonjs\\", \\"target\\": \\"ES6\\" }' ./path/to/MyFile.ts"
If that doesn't work, ditch ts-node and just use tsc & node ./dist/MyFile.js
如果这不起作用,请放弃 ts-node 并使用
tsc & node ./dist/MyFile.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.