Dynamic or static import of json in browser with ESM Dynamic or static import of json in browser with ESM google-chrome google-chrome

Dynamic or static import of json in browser with ESM


You can't directly import JSON using an ES6 import. You need to export it from a JS file:

// module.jsexport default {  "foo": { "bar": "baz" }};
// index.js;(async () => {  const mod = await import('/module.js')  console.log(mod)})()


Import Assertions are now stage 3 as of writing.

import json from "./foo.json" assert { type: "json" };// orimport("foo.json", { assert: { type: "json" } });

would be the syntax for pulling in a JSON file with ESM.