Error Require() of ES modules is not supported in Nodejs

Dung Do Tien Jun 27 2022 247

Hi Dev guys, I have written a small NodeJs application.  I want to fetch data from API and display it on the browser as a table. I installed node-fetch package and use it to fetch data from API. You can see it here:

index.js

 const fetch = require('node-fetch');
var express = require('express');
var app = express();

app.get('/', async (req, res) =>  {
        const result = await getUser();
        console.log(result);
});

var server = app.listen(4500, function () {
    console.log('Server is running..');
});


async function getUser() {
    try {
      const response = await fetch('https://randomuser.me/api/');
  
      if (!response.ok) {
        throw new Error(`Error! status: ${response.status}`);
      }
  
      const result = await response.json();
      return result;
    } catch (err) {
      console.log(err);
    }
}

I used node index.js command to run Node app, but I got an exception throw Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: "node-fetch\src\index.js". Require() of ES modules is not supported.

 Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\conta\source\repos\NodeJs\Demo1\node_modules\node-fetch\src\index.js
require() of ES modules is not supported.
require() of C:\Users\conta\source\repos\NodeJs\Demo1\node_modules\node-fetch\src\index.js from C:\Users\conta\source\repos\NodeJs\Demo1\index.js is an ES module file as it is a .js file whose nearest parent packag
e.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename C:\Users\conta\source\repos\NodeJs\Demo1\node_modules\node-fetch\src\index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\conta\source\repos\No
deJs\Demo1\node_modules\node-fetch\package.json.

←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:13)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:985:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:878:14)←[39m
←[90m    at Module.require (internal/modules/cjs/loader.js:1025:19)←[39m
←[90m    at require (internal/modules/cjs/helpers.js:72:18)←[39m
    at Object.<anonymous> (C:\Users\conta\source\repos\NodeJs\Demo1\index.js:1:15)
←[90m    at Module._compile (internal/modules/cjs/loader.js:1137:30)←[39m
←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:985:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:878:14)←[39m {
  code: ←[32m'ERR_REQUIRE_ESM'←[39m
}

Here is my package.json file:

 {
  "name": "Demo1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "dotenv": "^16.0.1",
    "express": "^4.18.1",
    "node-fetch": "^3.2.6"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Where am I going wrong?

Thank you in advance.

Have 2 answer(s) found.
  • L

    Lalan Kumar Jun 27 2022

    Based on your package.json file, you are using node-fetch version 3.  This version did not support require('node-fetch') syntax. 

    To solve this issue, you can:

    Step1:  add more "type": "module" into your package.json file.

     {
      ........
      "type": "module",
      ........
    }

    Step2:  Change

     const fetch = require('node-fetch');

    To

     import fetch from "node-fetch";

    It'll be solved for you.

  • a

    amit Jun 27 2022

    the error Require() of ES modules is not supported occurs because you installed node-fetch version 3 and you can downgrade and install version 2.

    To install node-fetch version 2, run the command:

     npm install [email protected]

    Your issue will be solved.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close