首页 > 其他分享 >JSON & import assertions All In One

JSON & import assertions All In One

时间:2023-03-27 09:44:10浏览次数:55  
标签:json JSON assertions https import com pkg

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"

*/


image

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

(

标签:json,JSON,assertions,https,import,com,pkg
From: https://www.cnblogs.com/xgqfrms/p/17260205.html

相关文章