Importing in a `type=module` project fails
See original GitHub issueFirst: thanks for the simplified version of state management. So glad I found it, so sad it took me so long 😉.
I created a small project, where the package.json
sets it to be a ESM project, by type=module
.
See https://codeberg.org/wolframkriesing/bug-exploration-zustand-and-esm/src/branch/main/package.json#L4
I ran into two issues, that are somehow related to #201 but I am not sure exactly what’s the root cause.
The Problem(s)
I wanted to get zustand to be testable with mocha in an ESM setup. But I ran into:
- When using
type=module
and importingzustand/vanilla
mocha says there is no such file. No idea what that might be. This is the JS file and the error it throws: https://codeberg.org/wolframkriesing/bug-exploration-zustand-and-esm/src/branch/main/src/vanilla.test.js - When importing
zustand/vanilla.js
the default export is somehow broken. A change ofexports.default = create;
to
module.exports = create;
inzustand/vanilla.js
would fix it. This sounds like a packager problem in zustand? https://codeberg.org/wolframkriesing/bug-exploration-zustand-and-esm/src/branch/main/src/vanilla-js.test.js
Is the ESM setup just not there yet, and not recommended? I had hoped to go there gradually. How do others solve this issue?
Issue Analytics
- State:
- Created 3 years ago
- Comments:33 (15 by maintainers)
Top Results From Across the Web
Importing pure ESM module in TS project fails Jest test with ...
Trying to use the file-type module which is pure ESM in a TS project but my jest fails. I have set the ESM...
Read more >Use ES Modules in Node.js & Fix "cannot use import ...
Fix cannot use import statement outside a module error in Node.js development ... In the package.json file, we need to add "type": "module"...
Read more >Modules, introduction - The Modern JavaScript Tutorial
In the example below, two scripts are imported, and hello.js tries to use user variable declared in user.js . It fails, because it's...
Read more >Modules: Packages | Node.js v19.3.0 Documentation
js, treated as an ES module because there is a package.json // file in the same folder with "type": "module". import './startup/init.
Read more >How to Bypass ES Modules Errors in Next.js with Dynamic ...
Error [ERR_REQUIRE_ESM ]: Must use import to load ES Module: ... package.json contains "type": "module" which defines all .js files in that ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sounds like a tricky one, not fun. What about a (challenging) suggestion: Why not ship ESM only? One file, only import/export, pure ESM. Why?
Sounds more like a major version thing, and it might prevent more compat work?
I don’t know of a pattern that would allow you to get rid of the preceding require. Node’s the limiting factor there in the keys it has implemented.
As for the dual hazard, I suppose the only way to tell is to test. I’ll try a few things out, see if I can get something to break.