JSON 不支持断言类型

问题描述 投票:0回答:1
node:internal/errors:464
9:07:48 PM:     ErrorCaptureStackTrace(err);
9:07:48 PM:     ^
9:07:48 PM: TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "file:///opt/build/repo/.contentlayer/generated/Blog/blog__BLANK.mdx.json" needs an import assertion of type 

但是我有 --experimental-json-modules 作为构建选项

当我删除它有效的有问题的 json 文件时,我真的很困惑,奇怪的是,我有一份时事通讯,它的工作原理与 .json 结尾非常相似,而且它有效。

node.js json yarnpkg
1个回答
0
投票

导入后您需要

assert

// "(node:3933) ExperimentalWarning: Import assertions are not a stable feature of the JavaScript language.
// Avoid relying on their current behavior and syntax as those might change in a future version of Node.js."
// The proposed official syntax is `with` instead of `assert`. https://github.com/tc39/proposal-import-attributes

import json_object from "./blog__BLANK.mdx.json" assert { type: "json" };
console.log("typeof json_object =", typeof json_object); // typeof json_object = object
© www.soinside.com 2019 - 2024. All rights reserved.