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.

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: lodash-es\lodash.js

See original GitHub issue

Describe the bug I’m trying to use lodash-es like this:

import { debounce } from 'lodash-es'

//...debounce using it

but I’m getting the below error.

Logs

1:37:22 AM [vite] Error when evaluating SSR module /src/routes/players/_cmp/SearchBar.svelte:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\kit\node_modules\lodash-es\lodash.js
require() of ES modules is not supported.
require() of C:\kit\node_modules\lodash-es\lodash.js from C:\kit\node_modules\vite\dist\node\chunks\dep-e9a16784.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 lodash.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\kit\node_modules\lodash-es\package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1085:13)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Module.require (internal/modules/cjs/loader.js:957:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at nodeRequire (C:\kit\node_modules\vite\dist\node\chunks\dep-e9a16784.js:68211:17)
    at ssrImport (C:\kit\node_modules\vite\dist\node\chunks\dep-e9a16784.js:68164:20)
    at eval (/src/routes/players/_cmp/SearchBar.svelte:7:31)
    at instantiateModule (C:\kit\node_modules\vite\dist\node\chunks\dep-e9a16784.js:68197:166)

To Reproduce Use lodash-es in a new svelte-kit skeleton (with init).

Information about your SvelteKit Installation:

Diagnostics
  • The output of npx envinfo --system --npmPackages svelte,@sveltejs/kit,vite --binaries --browsers:

    System: OS: Windows 10 10.0.19042 Binaries: Node: 14.17.0 - C:\Program Files\nodejs\node.EXE npm: 7.14.0 - C:\Program Files\nodejs\npm.CMD npmPackages: @sveltejs/kit: 1.0.0-next.109 => 1.0.0-next.109 svelte: 3.38.2 => 3.38.2 vite: 2.3.3 => 2.3.3

Severity This is really annoying.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
frederikhorscommented, May 27, 2021

@frederikhors I’ve had issues with lodash-es and have worked around the issue by sticking with version 4.17.15 instead of the latest 4.17.21 in the interim. I believe esm compatibility is a known issue with lodash.

I’ve also had issues with d3-format and have stayed on 2.0.0 instead of 3.0.0

Wow. All this is scary!

For now I’m using import debounce from 'lodash/debounce.js' instead of import { debounce } from 'lodash-es'.

0reactions
techniqcommented, May 27, 2021

@frederikhors I’ve had issues with lodash-es and have worked around the issue by sticking with version 4.17.15 instead of the latest 4.17.21 in the interim. I believe esm compatibility is a known issue with lodash.

I’ve also had issues with d3-format and have stayed on 2.0.0 instead of 3.0.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error ERR REQUIRE ESM | Must use import to load ES Module
JS - JavaScript. Error ERR REQUIRE ESM | Must use import to load ES Module. 15,379 views15K views. Apr 2, 2021. 46. Dislike....
Read more >
Error [ERR_REQUIRE_ESM]: require() of ES Module not ...
The node-fetch latest version doesn't use the require() syntax to import the package. You need to go to your package.json and type
Read more >
Error [ERR_REQUIRE_ESM]: Must use import to load ES ...
The error Error [ERR_REQUIRE_ESM]: Must use import to load ES Module occurs for 2 main reasons a module you are importing has been...
Read more >
ES Modules in Node.js - Maxim Orlov
Loading and parsing imported modules happens in parallel and asynchronously. The execution needs to be synchronous, but loading and parsing don't have to...
Read more >
Must use import to load ES Module - Help - Pipedream
AWS Lambda currently doesn't support ESM imports, so you've got to use dynamic imports / CommonJS requires (where supported) for all imports ......
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