语法错误:在 Angular 包格式项目中运行 ts-node 时出现意外的标记 {



我正在尝试运行带有ts-node的打字稿文件。 收到一个奇怪的错误:

import { tes } from "./tes";
^

语法错误:意外的令牌 { at 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 包格式库中。 这是顶级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,如果没有配置,节点就无法理解这些模块。您可以:

  1. tsconfig.json中删除"module": "esnext",线
  2. 或者,假设您想保留 Angular 的配置,请将您的节点配置为使用新的 ES 模块语法,例如按照此处的答案进行操作

最新更新