SyntaxError:意外令牌{在Angular Package Format项目中运行ts-node时

问题描述 投票:-1回答:1

我正在尝试使用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"
  ]
}


想法?

javascript angular typescript ts-node angular-package-format
1个回答
0
投票

看来您的tsconfig已使用esnext模块设置为输出js,如果没有配置,节点将无法理解。您可以:

  1. "module": "esnext",中删除tsconfig.json行>
  2. 或者,假设您想保留Angular的配置,请将您的节点配置为使用新的ES模块语法,例如通过关注the answer here
© www.soinside.com 2019 - 2024. All rights reserved.