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