close

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

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following Error [ERR_REQUIRE_ESM]: Must use import to load ES Module nodeJs 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 To Solve Error [ERR_REQUIRE_ESM]: Must use import to load ES Module nodeJs?

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

    To Solve Error [ERR_REQUIRE_ESM]: Must use import to load ES Module nodeJs All you have to do is adding the flag –experimental-modules that supports the new es6 import/export statement also the order is important as the following.

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

    To Solve Error [ERR_REQUIRE_ESM]: Must use import to load ES Module nodeJs All you have to do is adding the flag –experimental-modules that supports the new es6 import/export statement also the order is important as the following.

Solution 1

All you have to do is adding the flag –experimental-modules that supports the new es6 import/export statement also the order is important as the following.

    "start": "node --experimental-modules src/index.mjs "

Solution 2

short summary:

  1. your root level package.json doesn’t support ESM
  2. subdirectory does – in src directory place package.json with { "type": "module" }

PS: ESLint team can’t solve it easily right now, just because of core design… 🙁

Solution 3

You have to add this line of code in your package.json file "type" : "module" You will be able to use imports statements instead of require if I get your question correctly.

Your package.json will look as follows:

{
  "name": "dynamic-es6-mod",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.mjs",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node src/index.mjs"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "globby": "^10.0.1"
  }
}

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? Thank You.

Also, Read

Leave a Comment