JSON & import assertions All In One
JSON & import assertions All In One
error
// const packageInfo = require("./package.json");
import * as pkg from "./package.json";
console.log(`pkg `, pkg);
/*
TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "file:///Users/xgqfrms-mm/Documents/github/app-node-env/package.json" needs an import assertion of type "json"
*/

solution
// const packageInfo = require("./package.json");
import * as pkg from "./package.json" assert { type: "json" };
console.log(`pkg `, pkg);
/*
(node:33503) ExperimentalWarning: Importing JSON modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
*/
import assertions
https://v8.dev/features/import-assertions
https://github.com/tc39/proposal-import-attributes
blog
https://2ality.com/2021/01/import-assertions.html
https://2ality.com/2021/06/json-modules.html
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
https://www.cnblogs.com/xgqfrms/p/14927874.html
bug
https://github.com/whatwg/html/issues/7233
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17260205.html
未经授权禁止转载,违者必究!

浙公网安备 33010602011771号