1

我有一个看似简单的 npm 脚本失败了,但我似乎无法弄清楚原因。我在全球范围内安装了以下软件包:

+-- ts-node@8.3.0
+-- tsconfig-paths@3.8.0
+-- typeorm-model-generator@0.3.4
`-- typescript@3.5.2

当我从命令行运行ts-node时,它按预期运行。

在我的package.json文件中,我有:

"scripts": {
    "ts_test": "ts-node"
},

当我运行时,npm run ts_test我收到以下错误:

SyntaxError: Unexpected token } in JSON at position 581
    at JSON.parse (<anonymous>)
    at parse (...\node_modules\tsconfig\src\tsconfig.ts:195:15)
    at readFileSync (...\node_modules\tsconfig\src\tsconfig.ts:181:10)
    at Object.loadSync (...\node_modules\tsconfig\src\tsconfig.ts:151:18)
    at readConfig (...\node_modules\ts-node\src\index.ts:425:18)
    at Object.register (...\node_modules\ts-node\src\index.ts:189:18)
    at Object.<anonymous> (...\node_modules\ts-node\src\_bin.ts:140:17)
    at Module._compile (internal/modules/cjs/loader.js:721:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)

有关可能导致此错误的原因或如何进一步调试它的任何建议都会有所帮助。

更新:下面是我的 tsconfig 文件:

{
  "compilerOptions": {
    "lib": [
      "es2017"
    ],    
    "baseUrl": "/",
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": false,
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es2017",
    "outDir": "lib",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "esModuleInterop": true,
  },
  "exclude": [
    "node_modules"
  ]
}
4

1 回答 1

3

JSON 规范不支持尾随逗号。因此,您的解析tsconfig.json失败。改变你tsconfig.json

{
  "compilerOptions": {
    "lib": [
      "es2017"
    ],    
    "baseUrl": "/",
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": false,
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es2017",
    "outDir": "lib",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "esModuleInterop": true
  },
  "exclude": [
    "node_modules"
  ]
}
于 2019-07-01T13:46:53.840 回答