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

Hi,

Apologies in advance if this is a node issue and not a geolib issue, I’m not familiar with this error message.

Up to and including node v12.10.0 we have had no issue loading this library using the syntax:

const geolib = require('geolib');

since nodejs version v12.11.0 we are getting the following error:

internal/modules/cjs/loader.js:958
      throw new ERR_REQUIRE_ESM(filename);
      ^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/travis/build/pelias/api/node_modules/geolib/lib/index.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:958:13)
    at Module.load (internal/modules/cjs/loader.js:798:32)
    at Function.Module._load (internal/modules/cjs/loader.js:711:12)
    at Module.require (internal/modules/cjs/loader.js:838:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/travis/build/pelias/api/controller/placeholder.js:4:16)
    at Module._compile (internal/modules/cjs/loader.js:945:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:962:10)
    at Module.load (internal/modules/cjs/loader.js:798:32)
    at Function.Module._load (internal/modules/cjs/loader.js:711:12)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 18
  • Comments: 77 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Quite blocked by this one on Node v12.11.0. Seems like "type": "module" is just causing a hassle…

I have the same issue.

I just published 3.2.0. Can anyone of you please test it in your environment and tell me if it’s fixed? All I did was to remove the "type": "module" flag in package.json.

Anyway the problem in node 13 is still there, I have solved it by renaming geolib/lib/index.js to geolib/lib/index.cjs and changing the reference {"main": "lib/index.cjs"} in package.json

Anyway the problem in node 13 is still there

Indeed. I have the same issue in the Node.js current release: 13.2.0.

I think this is a node issue, I can’t even run npx create-react-app.

I am using node 12.16.2. It’s fine in 12.11.1

I have the exact same problem. Using Node 16.3.0

I’m using Node.js v14.16.1 and getting this error. I tried everything as described below(bold content) to remove this issue but still error is there. Help from you is highly appreciated.

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

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/Desktop/Node js project/COINDEX-CLI/node_modules/configstore/index.js require() of ES modules is not supported. require() of /Users/Desktop/Node js project/COINDEX-CLI/node_modules/configstore/index.js from /Users/Desktop/Node js project/COINDEX-CLI/lib/KeyManager.js is an ES module file as it is a .js file whose nearest parent package.json contains “type”: “module” which defines all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove “type”: “module” from /Users/Desktop/Node js project/COINDEX-CLI/node_modules/configstore/package.json.

at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/Users/Desktop/Node js project/COINDEX-CLI/lib/KeyManager.js:1:21)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14) {

code: ‘ERR_REQUIRE_ESM’ }

I have a same issue like this Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\Prasath\swagger-api\node_modules\swagger-jsdoc\index.js require() of ES modules is not supported. Give me the solution please.

Still broken in v14.4.0. I will try the solution above and check if it works for this version of node.

same problem, any updates ?

same problem. Using node v16.5.0

Same issue with Node.js release: 12.13.1 (LTS)

https://github.com/nodejs/node/pull/29909

I mean upgrade your geolib dependency to 3.2.x. The "type": "module" flag has been removed from 3.1.x -> 3.2.x

When removing “type”: “module” from package.json,

import _ from “lodash”; ^^^^^^

SyntaxError: Cannot use import statement outside a module

appears.

Me neither, I cant run npx create-react-app

Looks like the same issue got introduced in 12.16.0.

Same with Node v15.10.0

no success on various node versions , removing "type": "module" didn’t do anything.

Upgrading geolib from 3.0.x to 3.2.x fixed it for me.

Well, the best solution is to rename the index.js to index.cjs and change the reference in package.json to “main”: “index.cjs”,

same issue here

same issue here guys…but it worked for me earlier…

I am not able to run npx create-react-app hello-world

It says below error

Must use import to load ES Module: /usr/local/lib/node_modules/create-react-app/node_modules/is-promise/index.js require() of ES modules is not supported. require() of /usr/local/lib/node_modules/create-react-app/node_modules/is-promise/index.js from /usr/local/lib/node_modules/create-react-app/node_modules/run-async/index.js is an ES module file as it is a .js file whose nearest parent package.json contains “type”: “module” which defines all .js files in that package scope as ES modules. Instead rename /usr/local/lib/node_modules/create-react-app/node_modules/is-promise/index.js to end in .cjs, change the requiring code to use import(), or remove “type”: “module” from /usr/local/lib/node_modules/create-react-app/node_modules/is-promise/package.json.

I am using node 12.16.2 which I downloaded today

broken in node 13.3.0

Can we try to consolidate which versions of Node are affected here?

Working:

  • 12.11.1
  • 12.12.0

Broken:

  • 12.11.0
  • 12.13.1
  • 13.2.0

I just test it with node 12.11.1 and works fine. It was a problem with node 12.11.0: This release fixes a regression that prevented from building Node.js using the official source tarball (Richard Lau) https://nodejs.org/en/blog/release/v12.11.1/

Fixed it for me after upgrading node 12.16.3 -> 14.17.0

Getting the same on Node v15.13.0

Anyway the problem in node 13 is still there, I have solved it by renaming geolib/lib/index.js to geolib/lib/index.cjs and changing the reference {"main": "lib/index.cjs"} in package.json

after some time researching, it was the solution for me, thanks!

It’s working now guys, they have fixed it, I tested it in v12.16.2

this is the link to the issue: https://github.com/then/is-promise/pull/15

As I don’t have the geolib package as dependency I guess I should open an issue with gulp. I’m sorry, I probably came to the wrong page.

Alright, thanks. I think this can be considered done then. I’m currently investigating how this whole new ES Module loading mechanism in Node and modern browsers works (or not works). I have a rough high level idea but really not more than that. If you wanna follow me, I’m documenting everything here: https://github.com/manuelbieh/authoring-modules-in-node

Tested with Node 12.13.1 (LTS) successfully. Thanks!

Happy to hear that! 🙂

@ptboyer did you read the thread above? this is a known issue with v12.11.0 which was resolved in v12.11.1

Heya OP here, the originally reported issue was resolved in v12.11.1, sorry I should have closed it before that last comment came through…