Import a JSON file content from a Node.JS module

Emmanuel Gautier / May 22, 2023

1 min read

With the new Node.JS modules, it is possible to read JSON file content with the import function which is quite simple but you have to do it the right way.

If you just make as follow you will have the needs an import assertion of type "json" error:

example.mjs
import packageJSON from './package.json';

The reason is that you have to assert the type even if Node.js understand it is a JSON. According to tc39/proposal-import-attributes, you should write the import with an assertion like this:

example.mjs
import packageJSON from './package.json' assert { type: 'json' };

Consulting

If you're seeking solutions to a problem or need expert advice, I'm here to help! Don't hesitate to book a call with me for a consulting session. Let's discuss your situation and find the best solution together.

Share this post
Follow the RSS feed