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

esm uses process.node.env causing an error. #557

Closed
mmulet opened this issue Mar 20, 2020 · 4 comments
Closed

esm uses process.node.env causing an error. #557

mmulet opened this issue Mar 20, 2020 · 4 comments

Comments

@mmulet
Copy link

mmulet commented Mar 20, 2020

🐛 Bug Report

When using immer.esm.js on browser. We get
Uncaught ReferenceError: process is not defined

<script type="module" src="./testESM.js"></script>
import { produce } from "https://unpkg.com/[email protected]/dist/immer.esm.js";

Declaring the variable ahead of time fixes the issue.

<script>
  let process = {
    env: {
      NODE_ENV: "production"
    }
  };
</script>

Link to repro

Couldn't use code-sandbox because it wanted to import all modules as
node modules instead of as an esm module.
repo

To Reproduce

npm install . && npm test

Observed behavior

On import, throws
Uncaught ReferenceError: process is not defined

Expected behavior

Not throwing an exception

Environment

We only accept bug reports against the latest Immer version.

  • Immer version: latest
  • N/A Occurs with setUseProxies(true)
  • N/A Occurs with setUseProxies(false) (ES5 only)
@oriSomething
Copy link
Contributor

You've just used the wrong reference. I think using https://unpkg.com/[email protected]/dist/immer.umd.production.min.js will fix you the issue

@mmulet
Copy link
Author

mmulet commented Mar 21, 2020

Unfortunately, that reference doesn't export any functions and can't be used as an esm module.
So it won't work see this branch.
I've done some more digging, and it looks like the problem stems from tsdx, (which in turn uses the plugin babel-plugin-dev-expression).
It's replacing all instances of __DEV__ with the code incompatible with browsers.
There is even an open issue in the babel-plugin-dev-expression repo about supporting modern browsers.
I think this issue may be best solved upstream,

@mmulet
Copy link
Author

mmulet commented Mar 23, 2020

Closing this issue.
For those of you following along from home:
Instead of using npm run build to build immer, use tsdx build --name immer --env production --minify --format esm to create a browser compatible esm file.
The immer maintainers might consider distributing this file as well as the other esm build, but that would be a different issue/debate.

@mmulet mmulet closed this as completed Mar 23, 2020
@jeremychone
Copy link

jeremychone commented Apr 29, 2020

Just my 2 cents. Immer is a great approach to immutable, and something that naturally fit client and server. Having something special to handle between client and server does add friction and unfortunate workarounds. We are using rollup/typescript, and I am not sure how to use the umd version, and the default setting seems to pickup the esm version by default. Anyway, for now, we have the following workaround in our code, and had to put it very high in the call stack.

// WORKAROUND for immer.js esm (see https://github.com/immerjs/immer/issues/557)
window.process = {
	env: {
		NODE_ENV: "production"
	}
};

Hope to understand how to setup immer with the rollup/typescript. I might be missing something obvious.

jpchase added a commit to jpchase/lineup-tracker that referenced this issue Jul 10, 2020
After the Immer upgrade, the Karma unit tests would fail with:
"Uncaught ReferenceError: process is not defined at node_modules/immer/dist/immer.esm.js"

Tests are run using ES modules, without bundling or minifying. It seems
the assumption is the the Node env check would be compiled out.

Similar to the workaround in index.html, needed to init |window.process|
before any tests are run.

See immerjs/immer#557 (comment).
jpchase added a commit to jpchase/lineup-tracker that referenced this issue Jul 10, 2020
After the Immer upgrade, the Karma unit tests would fail with:
"Uncaught ReferenceError: process is not defined at node_modules/immer/dist/immer.esm.js"

Tests are run using ES modules, without bundling or minifying. It seems
the assumption is the the Node env check would be compiled out.

Similar to the workaround in index.html, needed to init |window.process|
before any tests are run.

See immerjs/immer#557 (comment).
robhudson added a commit to mozilla/glam that referenced this issue Sep 23, 2020
This is the last version that works with our rollup config w/o
introducing hacks by defining `window.process`.

See: immerjs/immer#557
WHenderson pushed a commit to WHenderson/svelte-immer-store that referenced this issue May 17, 2021
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

3 participants