close

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

Hello Guys, How are you all? Hope You all Are Fine. Today I face the following error Error [ERR_REQUIRE_ESM]: Must use import to load ES Module in nodejs. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

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

I am trying to import userField from my users.js file into my index.js file But I am facing following error.

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

Here is my users.js

export let userField = ['username', 'email', 'dob', 'firstname', 'lastname'];

Here is my index.js

import { userField } from './users.js';
console.log(userField);

How To Solve Error [ERR_REQUIRE_ESM]: Must use import to load ES Module Error ?

  1. How To Solve Error [ERR_REQUIRE_ESM]: Must use import to load ES Module Error?

    To Solve Error [ERR_REQUIRE_ESM]: Must use import to load ES Module Error This code is perfect, But I recommend you should use the latest version of the node. I ran your code on my PC and working Fine. Just remember Node version >= 14. It only works with the latest version of the node.

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

    To Solve Error [ERR_REQUIRE_ESM]: Must use import to load ES Module Error This code is perfect, But I recommend you should use the latest version of the node. I ran your code on my PC and working Fine. Just remember Node version >= 14. It only works with the latest version of the node.

Solution 1: Just Use latest version of node

This code is perfect, But I recommend you should use the latest version of the node. I ran your code on my PC and working Fine. Just remember Node version >= 14. It only works with the latest version of the node.

Solution 2: add “type”: “module” in your package.json

Make sure your package.json includes a line for “type”: “module”, Without this line node assumes you want to use Common JS modules rather than ESM.

Solution 3: Change node version

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

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?

Also Read

Leave a Comment