首页 > 解决方案 > Angular 构建抛出错误 (JSON.parse(stripJsonComments(content)))

问题描述

我正在使用带有nestjs的角度nx。当项目被克隆并在其上运行 yarn 命令时,它会成功构建,但是当我安装任何包并编译项目时。它抛出错误:

**D:\projectNAme\nrwl\node_modules\@nrwl\workspace\src\utils\fileutils.js:44
    return JSON.parse(stripJsonComments(content));
                ^
SyntaxError: Unexpected token } in JSON at position 562
    at JSON.parse (<anonymous>)
    at Object.parseJsonWithComments (D:\projectNAme\nrwl\node_modules\@nrwl\workspace\src\utils\fileutils.js:44:17)
    at new TargetProjectLocator (D:\projectNAme\nrwl\node_modules\@nrwl\workspace\src\core\target-project-locator.js:22:46)
    at buildExplicitTypeScriptDependencies (D:\projectNAme\nrwl\node_modules\@nrwl\workspace\src\core\project-graph\build-dependencies\explicit-project-dependencies.js:8:34)
    at D:\projectNAme\nrwl\node_modules\@nrwl\workspace\src\core\project-graph\project-graph.js:60:41
    at Array.forEach (<anonymous>)
    at buildProjectGraph (D:\projectNAme\nrwl\node_modules\@nrwl\workspace\src\core\project-graph\project-graph.js:60:26)
    at Object.createProjectGraph (D:\projectNAme\nrwl\node_modules\@nrwl\workspace\src\core\project-graph\project-graph.js:40:30)
    at Object.<anonymous> (D:\projectNAme\nrwl\node_modules\@nrwl\workspace\src\command-line\run-one.js:16:46)
    at Generator.next (<anonymous>)
[21:31:39] 'deployLocalAPI' errored after 820 ms
[21:31:39] Error: Command failed: cd nrwl && nx serve api
    at checkExecSyncError (node:child_process:636:11)
    at Object.execSync (node:child_process:672:15)
    at Object.exec (D:\projectNAme\/gulpfile.babel.js:36:8)
    at D:\projectNAme\_config\gulp\tasks\deploy\apps\api\/deployLocal.js:41:10
    at deployLocalAPI (D:\projectNAme\node_modules\undertaker\lib\set-task.js:13:15)
    at bound (node:domain:413:15)
    at runBound (node:domain:424:12)
    at asyncRunner (D:\projectNAme\node_modules\async-done\index.js:55:18)
    at processTicksAndRejections (node:internal/process/task_queues:75:11)**

以下是我正在使用的版本:

"@angular-devkit/build-angular": "~0.1100.1",
"@angular/cli": "~11.0.0",
"@angular/compiler-cli": "^11.0.0",
"@angular/language-service": "^11.0.0",
"@nestjs/schematics": "^7.0.0",
"@nestjs/testing": "^7.0.0",
"@nrwl/cli": "11.0.2",
"@nrwl/cypress": "11.0.2",
"@nrwl/eslint-plugin-nx": "11.0.2",
"@nrwl/jest": "11.0.2",
"@nrwl/nest": "11.0.2",
"@nrwl/node": "11.0.2",
"@nrwl/tao": "11.0.2",
"@nrwl/workspace": "11.0.2",
"@types/jest": "26.0.8",
"@types/node": "12.12.38",
"@typescript-eslint/eslint-plugin": "4.3.0",
"@typescript-eslint/parser": "4.3.0",
"codelyzer": "^6.0.0",
"cypress": "^5.5.0",
"dotenv": "6.2.0",
"eslint": "7.10.0",
"eslint-config-prettier": "6.0.0",
"jest": "26.2.2",
"jest-preset-angular": "8.3.1",
"prettier": "2.1.2",
"ts-jest": "26.4.0",
"ts-node": "~7.0.0",
"tslint": "~6.0.0",
"typescript": "~4.0.3"

标签: node.jsangularnrwlnomachine-nx

解决方案


当我们无意中忘记了项目中 .json 文件中 JSON 对象末尾的逗号时,就会出现此问题。通常,它发生在编辑 tsconfig.json 之后。

由于那个孤独和不必要的逗号,JSON 解析器无法“找到”或“理解”右大括号,因为它正在等待另一个 JSON 属性,因此错误:

“JSON 中的意外令牌 }”

这是一个应该为 tsconfig.json 产生此类错误的示例(例如,在带有 VSCode 的 Angular 11 项目中):

"angularCompilerOptions": {
    "strictTemplates": true,
    "fullTemplateTypeCheck": true,<----- this last comma might be the culprit
  },

解决方案,检查您最近编辑的 json 文件结构的完整性,看是否有逗号或大括号丢失。如果它确实是任何孤独的逗号,只需删除它并保存文件就足够了。


推荐阅读