SyntaxError Unexpected identifier assert on JSON import in Node v22
在 Node v22 中导入 JSON 时出现 "SyntaxError: Unexpected identifier 'assert'" 错误
# 问题背景:
A Node program of mine that worked fine in Node v21 now immediately crashes with a syntax error in Node v22. The line that triggers the error looks like this:
我在 Node v21 中正常工作的一个 Node 程序,现在在 Node v22 中立即因语法错误崩溃。触发错误的代码行如下所示:
import config from "./some-config-file.json" assert { type: "json" }; | |
and the error says: 错误信息: | |
SyntaxError: Unexpected identifier 'assert' | |
at compileSourceTextModule (node:internal/modules/esm/utils:337:16) | |
at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:166:18) | |
at callTranslator (node:internal/modules/esm/loader:436:14) | |
at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:442:30) | |
at async ModuleJob._link (node:internal/modules/esm/module_job:106:19) | |
Why am I getting this error in code that previously worked, and how should I fix it? |
为什么我在以前正常工作的代码中会遇到这个错误,我应该如何修复它?