How to import JSON files in ES modules (Node.js) (#snippet)

ES modules are still reasonably new in Node.js land (they’re stable since Node 14). Modules come with a built-in module system and features such as top-level await.
I just read an informative post on ES modules by Pawel Grzybek and …


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

ES modules are still reasonably new in Node.js land (they're stable since Node 14). Modules come with a built-in module system and features such as top-level await.

I just read an informative post on ES modules by Pawel Grzybek and learned that you can't import JSON files in ES modules today. That's a real bummer because I'm pretty used to doing const data = require('./some-file.json') in Node.js.

While it will be possible to import JSON from within modules eventually, the implementation is still behind a flag (--experimental-json-modules).

This post includes ways to deal with JSON in ES modules today.

Option 1: Read and parse JSON files yourself

The Node.js documentation advises to use the fs module and do the work of reading the files and parsing it yourself.

import { readFile } from 'fs/promises';
const json = JSON.parse(
  await readFile(
    new URL('./some-file.json', import.meta.url)
  )
);

Option 2: Leverage the CommonJS require function to load JSON files

The documentation also states that you can use createRequire to load JSON files. This is the way Pawel advises in his blog post.

createRequire allows you to construct a CommonJS require function so that you can use typical CommonJS features in Node.js EcmaScript modules.

import { createRequire } from "module";
const require = createRequire(import.meta.url);
const data = require("./data.json");

How should you load JSON files?

I don't know. ?‍♂️ Neither option feels good, and I'll probably stick to the first option because it's more understandable.

Let's see when stable JSON modules land in Node.js!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2021-05-15T22:00:00+00:00) How to import JSON files in ES modules (Node.js) (#snippet). Retrieved from https://www.scien.cx/2021/05/15/how-to-import-json-files-in-es-modules-node-js-snippet/

MLA
" » How to import JSON files in ES modules (Node.js) (#snippet)." Stefan Judis | Sciencx - Saturday May 15, 2021, https://www.scien.cx/2021/05/15/how-to-import-json-files-in-es-modules-node-js-snippet/
HARVARD
Stefan Judis | Sciencx Saturday May 15, 2021 » How to import JSON files in ES modules (Node.js) (#snippet)., viewed ,<https://www.scien.cx/2021/05/15/how-to-import-json-files-in-es-modules-node-js-snippet/>
VANCOUVER
Stefan Judis | Sciencx - » How to import JSON files in ES modules (Node.js) (#snippet). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/15/how-to-import-json-files-in-es-modules-node-js-snippet/
CHICAGO
" » How to import JSON files in ES modules (Node.js) (#snippet)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2021/05/15/how-to-import-json-files-in-es-modules-node-js-snippet/
IEEE
" » How to import JSON files in ES modules (Node.js) (#snippet)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2021/05/15/how-to-import-json-files-in-es-modules-node-js-snippet/. [Accessed: ]
rf:citation
» How to import JSON files in ES modules (Node.js) (#snippet) | Stefan Judis | Sciencx | https://www.scien.cx/2021/05/15/how-to-import-json-files-in-es-modules-node-js-snippet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.