SyntaxError: Unexpected token { when running ts-node in Angular Package Format project

SyntaxError: Unexpected token { when running ts-node in Angular Package Format project

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

 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,如果没有配置,节点无法理解这些模块。您可以:

  1. tsconfig.json
  2. 中删除 "module": "esnext",
  3. 或者,假设您想保留 Angular 的配置,配置您的节点以使用新的 ES 模块语法,例如通过关注