Typescript 中的语法错误:意外的标记“/”在 JSON 中的 x 位置(带注释的 JSON)

发布于 2025-01-16 19:49:57 字数 1380 浏览 2 评论 0原文

最近,我考虑在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话 2025-01-23 19:49:57

VSCode 实际上并没有编译您的代码。它假设您正在预编译或预解析 json。

您的 Node 版本正在编译您的程序,并且 Node 不支持开箱即用的 JSON 注释。您将需要一个外部库:

https://www.npmjs.com/package/jsonminify

在项目根目录中,在命令行上运行以下命令:

npm install jsonminify

然后在 .js 文件中需要该库,并将任何 JSON.parse(myJSONString) 语句替换为JSON.parse(jsonminify(myJSONString))

jsonminify README.md 中的示例:

var jsonminify = require("jsonminify");

jsonminify('{"key":"value"/** comment **/}')
>> '{"key":"value"}'

JSON.minify('{"key":"value"/** comment **/}')
>> '{"key":"value"}'

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:

npm install jsonminify

Then require the library in your .js file and replace any JSON.parse(myJSONString) statements with JSON.parse(jsonminify(myJSONString))

Example from the jsonminify README.md:

var jsonminify = require("jsonminify");

jsonminify('{"key":"value"/** comment **/}')
>> '{"key":"value"}'

JSON.minify('{"key":"value"/** comment **/}')
>> '{"key":"value"}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文