Importing in Node.js: error "Must use import to load ES Module"

Javascriptnode.js

Javascript Problem Overview


I'm trying to import myArr from hello.js into index.js. However I get an error of

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module

File hello.js

export let myArr = ['hello', 'hi', 'hey'];

File index.js

import { myArr } from './hello.js';
console.log(myArr);

Where am I going wrong?

Javascript Solutions


Solution 1 - Javascript

Use version 2:

npm install node-fetch@2

node-fetch from v3 is an ESM-only module - you are not able to import it with require().

If you cannot switch to ESM, please use v2 which remains compatible with CommonJS. Critical bug fixes will continue to be published for v2.

Solution 2 - Javascript

I ran your code without any problems. Check for two things:

  1. Node.js version >= 14. It only works with the latest version of Node.js.
  2. Make sure your package.json includes a line for "type": "module". Without this line, Node.js assumes you want to use CommonJS modules rather than ESM.

Solution 3 - Javascript

I ran into a similar issue while building my React project.

Here's the error:

ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/pradeep/Work/my_project/node_modules/@babel/runtime/helpers/interopRequireWildcard/_index.mjs

Then I realized that I am on a different Node.js version than the one used to install packages in this project.

I had two options:

  1. Change Node.js version to the one required by this project and build again.
  2. Stay on the Node.js version you have and remove the node_modules directory and package-lock.json file and do npm install again.

I chose the first approach and it worked for me.

Solution 4 - Javascript

If you have Node.js version lower than 14, e.g., v12 - you must specify this flag:

node --experimental-modules your.mjs

Solution 5 - Javascript

I use nvm to install the latest stable version of Node.js.

To delete the package-lock.json file and the node_modules folder, run npm. I then npm start to solve my problem.

Solution 6 - Javascript

Rename hello.js to hello.mjs.

You are using CommonJS right now with the .js extension. To import ES6 modules, you will have to change the extension of the file that you want to import to .mjs so it knows that it is an ES6 module.

Solution 7 - Javascript

The problem is that Node.js does not currently support import and export natively yet. It is still experimental according to the documentation. I recommend you use Babel to compile your code and allow you to use import and export.

For example, you can install the @babel/node package and run your project using:

npx babel-node index.js

Here are the documentation for @babel/node. Like the documentation state, this command is only meant for local development. In production, they recommend a configuration like this.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElitezenView Question on Stackoverflow
Solution 1 - JavascriptmalView Answer on Stackoverflow
Solution 2 - JavascriptG ClarkView Answer on Stackoverflow
Solution 3 - JavascriptPradeep VigView Answer on Stackoverflow
Solution 4 - JavascriptDaniel GarmoshkaView Answer on Stackoverflow
Solution 5 - JavascriptAbdelwahed HouboubyView Answer on Stackoverflow
Solution 6 - JavascriptAviv LoView Answer on Stackoverflow
Solution 7 - JavascriptjjggView Answer on Stackoverflow