"type": "module" in package.json throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath)
Asked Answered
T

9

13

I want to use import in my nodejs project instead of using require. So, I added,

"type": "module"

in my package.json.

import index from './index.js';

in server.js

when I run

node server.js

Error says,

internal/modules/cjs/loader.js:1174
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

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

server.conf.js is pasted below.

import express from 'express';

import http from 'http';
let app = express();
let server = http.createServer(app);

import morgan from 'morgan';
import methodOverride from 'method-override';;
import path from 'path';

let port = process.env.PORT || 4000;
app.use(morgan('dev'));
app.use(methodOverride('X-HTTP-Method-Override'));
let router = express.Router();
import routes from '../app/routes';
routes(app, router, client);
server.listen(port);
console.log(`Wizardry is afoot on port ${port}`);
export {
    app,
    client
};
Tumbling answered 2/5, 2020 at 11:45 Comment(3)
Could you also paste the content of server.conf.js?Evvy
added 'server.conf.js'Tumbling
Does this answer your question? Error: require() of ES modules is not supported when importing node-fetchTerryn
E
4

According to stack-trace before you edit (https://stackoverflow.com/revisions/61558835/1):

internal/modules/cjs/loader.js:1174
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: H:\WORKSPACE\CMDs\node-basic\server.conf.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1174:13)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'ERR_REQUIRE_ESM'
}

I tried to locate the Node src who throws this error:

https://github.com/nodejs/node/blob/c24b74a7abec0848484671771d250cfd961f128e/lib/internal/modules/cjs/loader.js#L1234

// Native extension for .js
Module._extensions['.js'] = function(module, filename) {
  if (filename.endsWith('.js')) {
    const pkg = readPackageScope(filename);
    // Function require shouldn't be used in ES modules.
    if (pkg && pkg.data && pkg.data.type === 'module') {
      // ...
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
    }
  }
  // ...
};

The comment Function require shouldn't be used in ES modules tells the js file to be loaded is an ES module, but the caller is trying to use require() function to load it.

Moreover, a double-check into Node src https://github.com/nodejs/node/blob/6cc94b2d7f69f1f541f7c5de3cb86e569fbd4aa3/lib/internal/errors.js#L1319 proves that H:\WORKSPACE\CMDs\node-basic\server.conf.js is the ES module to be loaded.

So I'm trying to guess who is trying to load server.conf.js in your app but no luck. Most likely there is a require('./server.conf.js') in your index.js or somewhere else. If you find it just change it into import to fix.

Evvy answered 1/6, 2020 at 5:7 Comment(0)
M
12

For my case I downgrade: node-fetch ^3.0.0 → ^2.6.1

Problem solved.

Moreau answered 3/10, 2021 at 12:51 Comment(0)
E
4

According to stack-trace before you edit (https://stackoverflow.com/revisions/61558835/1):

internal/modules/cjs/loader.js:1174
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: H:\WORKSPACE\CMDs\node-basic\server.conf.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1174:13)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'ERR_REQUIRE_ESM'
}

I tried to locate the Node src who throws this error:

https://github.com/nodejs/node/blob/c24b74a7abec0848484671771d250cfd961f128e/lib/internal/modules/cjs/loader.js#L1234

// Native extension for .js
Module._extensions['.js'] = function(module, filename) {
  if (filename.endsWith('.js')) {
    const pkg = readPackageScope(filename);
    // Function require shouldn't be used in ES modules.
    if (pkg && pkg.data && pkg.data.type === 'module') {
      // ...
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
    }
  }
  // ...
};

The comment Function require shouldn't be used in ES modules tells the js file to be loaded is an ES module, but the caller is trying to use require() function to load it.

Moreover, a double-check into Node src https://github.com/nodejs/node/blob/6cc94b2d7f69f1f541f7c5de3cb86e569fbd4aa3/lib/internal/errors.js#L1319 proves that H:\WORKSPACE\CMDs\node-basic\server.conf.js is the ES module to be loaded.

So I'm trying to guess who is trying to load server.conf.js in your app but no luck. Most likely there is a require('./server.conf.js') in your index.js or somewhere else. If you find it just change it into import to fix.

Evvy answered 1/6, 2020 at 5:7 Comment(0)
J
3

Had the same issue. I installed the latest node version and then it worked. Try the same. Also if you are using windows make sure it is the correct version i.e 64-bit, 32-bit.

Jinx answered 14/9, 2020 at 5:52 Comment(0)
S
0

Check your NodeJS version for module compatibility("type": "module"), there are known issues on certain versions.

Most likely there is a require('./server.conf.js') in your index.js/server.js or in the dependent packages you are importing or somewhere else. If you find it just change it into import to fix.

1- Check you're all require statements

2- analyze dependent packages for a require statement in that code

  • Try a build ...
  • Try to deploy to NodeJS containers on GC, DO, AWS or HKU
Sepalous answered 30/12, 2020 at 17:1 Comment(0)
B
0

in my case i had a data file (data.js) with products listed as objects inside an array. looked like this :

const data={
  products:[
    {
      brand:'nike',
      price:1200
    },
    {
      brand:'adidas',
      price:1400
    }
  ]
}

export default data

THE ERROR was caused because i FOOLISHLY exported it like it was a function or class and wrote:

export default data={
etc...
}

i DOUBT this is a case of your error BUT it shows nonetheless how cumbersome and often this error can show up. if its any clarity what im trying to say im basically saying that this usually shows up due to a file itself being unreadable from import. if you put "type": "module" then it is def a version of node, OR a problem on a base level with something you are trying to import. try deleting each of the imports one by one initially to see which one may be the cause. then work from there

Bravery answered 31/1, 2021 at 19:4 Comment(0)
V
0

Nothing fancy needed. Just update the Node.js version.

Vostok answered 7/4, 2021 at 10:41 Comment(1)
Thanks! In my case it helped to update Node from version 12 to 16 by installing if from nodejs.org/enRigsdaler
C
0

I was also facing similar issue. So I downgrade chalk module version from 5.0.1 to 4.1.0. It worked for me.

Chaperone answered 1/4, 2022 at 7:4 Comment(0)
M
0

In my case I was running Angular 13.X och Nx 14.X but my Node version was still 12.X so upgrading the Node version to ^14 solves the problem.

Matrilineal answered 3/11, 2022 at 16:33 Comment(0)
C
0

I updated the terminal node version to 16, deleted node_modules and installed it again. And fixed.

Claraclarabella answered 2/12, 2022 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.