我正在尝试使用ts-node
运行打字稿文件。收到一个奇怪的错误:
import { tes } from "./tes";
^
SyntaxError:意外令牌{在Module._compile(internal / modules / cjs / loader.js:720:23)
如果我将项目ts
文件复制到一个空目录中,并尝试在不进行任何配置的情况下运行该文件,则该文件可以正常运行,因此它已在配置中完成。
我也尝试过生成一个全新的角度包格式项目:
ng new proj --create-application=false
cd proj
ng g library x
如果删除与项目和库关联的所有tsconfig
文件,则ts-node
运行正常。当我将它们留在里面时,会出现相同的错误。
tes
文件在Angular Package Format库中。这是顶层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,如果没有配置,节点将无法理解。您可以:
"module": "esnext",
中删除tsconfig.json
行>