question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Must use import to load ES Module" problem

See original GitHub issue

With node 12 , when I tries the sample I got the following error. It looks like it cannot import the module. Does it support node 12?

$> node -r esm
Welcome to Node.js v12.16.1.
> import LatLon from 'geodesy/latlon-spherical.js';
.../node_modules/geodesy/latlon-spherical.js:1
Uncaught:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: .../node_modules/geodesy/latlon-spherical.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1174:13) {
  code: 'ERR_REQUIRE_ESM'
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
strarsiscommented, Nov 25, 2020

I get the same error with node v15.3.0 and with the recommended flags enabled.

node --experimental-modules -r esm test.js
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: [...]/node_modules/geodesy/latlon-ellipsoidal-vincenty.js
    at new NodeError (node:internal/errors:278:15)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1119:13) {
  code: 'ERR_REQUIRE_ESM'
}
0reactions
mickeyjohncommented, Nov 19, 2021

Hi,

I got the same error message when trying to use V2 on CommonJs modules. Thus, I migrated to using v2 with some code changes as examples below. Basically, in recent Node versions > v12.x, you can use dynamic module loading directly. Note that dynamic ES modules loading works asynchronously.

async function Func() {
  // Dynamically load ES geodesy v2 modules.
  const llsMod = await import('geodesy2/latlon-spherical.js');
  LatLonSP = llsMod.default;
  const utmMod = await import('geodesy2/utm.js');
  Utm = utmMod.default;
  LatLon = utmMod.LatLon;

 // Then use as:
  const utm1 = new Utm(..);
  const latlng = new LatLon();
  ... 
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Importing in Node.js: error "Must use import to load ES Module"
The problem is that Node.js does not currently support import and export natively yet. It is still experimental according ...
Read more >
Error: Must use import to load ES Module · Issue #1956 - GitHub
After update GOT library to version 12.0.0 I've got an error: Error: Must use import to load ES Module: .
Read more >
Must use import to load ES Module - Help - Pipedream
Hi, suddenly my workflow started to fail with the following error: “Must use import to load ES Module”. I know there's a similar...
Read more >
Must use import to load ES Module - Netlify Support Forums
Error [ERR_REQUIRE_ESM ]: Must use import to load ES Module: /var/task/node_modules/@steeze-ui/heroicons/index.js require() of ES modules is ...
Read more >
Must use import to load ES Module - Auth0 Community
JS. When I follow the error instructions and use import I get “Cannot use import statement outside a module”. So I fell back...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found