我正在尝试使用 ts-node 运行 typescript 文件。收到一个奇怪的错误:
import { tes } from "./tes";
^
SyntaxError: Unexpected token { at Module._compile (internal/modules/cjs/loader.js:720:23)
如果我将项目 ts
文件复制到一个空目录中并尝试在没有任何配置的情况下运行该文件,它可以正常工作,因此它是配置中的内容。
我还尝试生成一个全新的 Angular 包格式项目:
ng new proj --create-application=false
cd proj
ng g library x
如果我删除与项目和库关联的所有 tsconfig
文件,ts-node
运行正常。当我把它们留在里面时,我得到了同样的错误。
tes
文件位于 Angular 包格式库内。这是顶层 tsconfig.json
:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"lib": [
"es2018",
"dom"
],
"paths": {
"csv": [
"dist/csv/csv",
"dist/csv"
]
}
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
这是项目特定的tsconfig.lib.json
:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/lib",
"target": "es2015",
"declaration": true,
"inlineSources": true,
"types": [],
"lib": [
"dom",
"es2018"
]
},
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"enableResourceInlining": true
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
想法?
最佳答案
看起来你的 tsconfig 设置为使用 esnext 模块输出 js,如果没有配置,节点无法理解这些模块。您可以:
- 从
tsconfig.json
中删除"module": "esnext",
行 - 或者,假设您想保留 Angular 的配置,请将您的节点配置为使用新的 ES 模块语法,例如通过关注 the answer here
关于javascript - 语法错误: Unexpected token { when running ts-node in Angular Package Format project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61926214/