Typescript 中的语法错误:意外的标记“/”在 JSON 中的 x 位置(带注释的 JSON)
最近,我考虑在 JSON 文件中使用注释,并在 VSCode 中发现了带有注释的 JSON。
我将 JSON 文件切换为带注释的 JSON。这并没有造成任何麻烦。
但是,当我编译时,它显示错误:
node:internal/modules/cjs/loader:1170
throw err;
^
SyntaxError: C:\...\data\materialData.json: Unexpected token / in JSON at position 7
at parse (<anonymous>)
at Object.Module._extensions..json (node:internal/modules/cjs/loader:1167:22)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:94:18)
at Object.<anonymous> (C:\Users\Jack\Documents\Jacks_Workshop\Coding\DiscordBot_v4\jsons.js:17:43)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
这是我的 tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"downlevelIteration": true,
"strict": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true
}
}
Recently, I've pondered using comments in JSON files and discovered JSON with Comments in VSCode.
I switched my JSON files to JSON with Comments. That didn't create any trouble.
However, when I compile, it shows the error:
node:internal/modules/cjs/loader:1170
throw err;
^
SyntaxError: C:\...\data\materialData.json: Unexpected token / in JSON at position 7
at parse (<anonymous>)
at Object.Module._extensions..json (node:internal/modules/cjs/loader:1167:22)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:94:18)
at Object.<anonymous> (C:\Users\Jack\Documents\Jacks_Workshop\Coding\DiscordBot_v4\jsons.js:17:43)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
Here is my tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"downlevelIteration": true,
"strict": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
VSCode 实际上并没有编译您的代码。它假设您正在预编译或预解析 json。
您的 Node 版本正在编译您的程序,并且 Node 不支持开箱即用的 JSON 注释。您将需要一个外部库:
https://www.npmjs.com/package/jsonminify
在项目根目录中,在命令行上运行以下命令:
然后在 .js 文件中需要该库,并将任何
JSON.parse(myJSONString)
语句替换为JSON.parse(jsonminify(myJSONString))
jsonminify README.md 中的示例:
VSCode isn't actually compiling your code. It assumes you are pre-compiling or pre-parsing your json.
Your version of Node is compiling your program and Node doesn't support comments in JSON out of the box. You will need an external library for that:
https://www.npmjs.com/package/jsonminify
In the project root, run this on the command line:
Then require the library in your .js file and replace any
JSON.parse(myJSONString)
statements withJSON.parse(jsonminify(myJSONString))
Example from the jsonminify README.md: