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.

Importing in a `type=module` project fails

See original GitHub issue

First: 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:

  1. When using type=module and importing zustand/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
  2. When importing zustand/vanilla.js the default export is somehow broken. A change of exports.default = create;
    to
    module.exports = create;
    in zustand/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:closed
  • Created 3 years ago
  • Comments:33 (15 by maintainers)

github_iconTop GitHub Comments

4reactions
wolframkriesingcommented, Apr 11, 2021

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?

  1. this is where JS is going
  2. let the user transpiler if they need it (anyways)
  3. much simpler to maintain

Sounds more like a major version thing, and it might prevent more compat work?

1reaction
rschristiancommented, Apr 11, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

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