Angular 构建抛出错误 (JSON.parse(stripJsonComments(content)))

Posted

技术标签:

【中文标题】Angular 构建抛出错误 (JSON.parse(stripJsonComments(content)))【英文标题】:Angular build throwing error (JSON.parse(stripJsonComments(content))) 【发布时间】:2021-04-10 11:34:46 【问题描述】:

我正在使用带有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"

【问题讨论】:

【参考方案1】:

当我们无意中忘记了项目中 .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 文件结构的完整性,以查找是否存在单独的逗号或丢失的大括号。如果它确实是一个单独的逗号,只需将其删除并保存文件就足够了。

【讨论】:

以上是关于Angular 构建抛出错误 (JSON.parse(stripJsonComments(content)))的主要内容,如果未能解决你的问题,请参考以下文章

Angular 8 ng-build 使用科尔多瓦抛出 MIME 错误

部署后 Socket.io 不工作 - 抛出错误 - 405(不允许)

从服务器抛出的自定义错误未在客户端的 HttpErrorResponse 中返回

Angular2 QuickStart 拒绝为候选发布版本构建

使用 Nginx 为 Angular2 CLI 构建的应用程序提供服务会抛出 404、403

在 Angular 应用程序中抛出错误