Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target ESM over CommonJS #117

Open
kasparkallas opened this issue Jul 26, 2023 · 5 comments
Open

Target ESM over CommonJS #117

kasparkallas opened this issue Jul 26, 2023 · 5 comments

Comments

@kasparkallas
Copy link

Currently the module target is set as CommonJS.

Nowadays ESM is a lower common denominator than CommonJS as it's supported in both Node and browser runtimes.

I have a pure ESM library which I serve unbundled (only tsc output) and the usage of the use-immer's require causes some trouble.

@mweststrate
Copy link
Collaborator

PR welcome

@irishcarbomb777
Copy link

I just ran into this issue also. I solved it by setting {"type": "module"} in the package.json for node_modules/use-immer (In other words for anyone else with the issue, add that line to the package.json of the installed use-immer package within the node_modules folder). I've never had an issue with the package until building a react-based 'ink' cli tool for node.

@Seize-cf
Copy link

Seize-cf commented Nov 2, 2023

It appears that microbundle may not have a dependency on tsconfig.json. Perhaps changing the module to ESNext could potentially solve the issue.

#119

@k1sul1
Copy link

k1sul1 commented Mar 7, 2024

PR welcome

The way I see it, there's 2 PRs open regarding this. I don't know or care if they work to solve the problem, but I see that they don't have any comments or any signs life really.

I will not be making a 3rd just to also get ignored.

I just ran into this issue also. I solved it by setting {"type": "module"} in the package.json for node_modules/use-immer (In other words for anyone else with the issue, add that line to the package.json of the installed use-immer package within the node_modules folder). I've never had an issue with the package until building a react-based 'ink' cli tool for node.

Thanks for this. Simply adding "module": true to the package.json in node_modules/use-immer and then rebuilding did the trick.

Went from this error

import { useImmerReducer } from "use-immer";
         ^^^^^^^^^^^^^^^
SyntaxError: Named export 'useImmerReducer' not found. The requested module 'use-immer' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'use-immer';
const { useImmerReducer } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at ModuleJob.run (node:internal/modules/esm/module_job:190:5)

to no error in 2 minutes.

I'm using this on top of a Remix 2.8 project, FWIW. We tried to migrate to ESM almost six months ago. We failed, because of this package. It was the only dependency that did not play well with ESM. Now we're in a situation where we can't keep using CJS as we're migrating to Vite.

I have fixed the problem for now using patch-package, but I guess I'm off to finding another solution, since useImmer is unusable in 2024, and displays no signs of that changing.

I'm fine with this solution. I just find it kinda sad that I have to do this.

wget -O hooks/useImmer.ts https://raw.githubusercontent.com/immerjs/use-immer/master/src/index.ts

@atsolberg
Copy link

@k1sul1 I was able to fix this by telling remix to bundle it with the server:
image

I love immer and I hope this package gets some ESM love someday. Thank you @mweststrate for all your hard work over the years!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants