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

Can't import node builtins in a route module #114

Closed
ryanflorence opened this issue Apr 12, 2021 · 9 comments
Closed

Can't import node builtins in a route module #114

ryanflorence opened this issue Apr 12, 2021 · 9 comments
Assignees

Comments

@ryanflorence
Copy link
Member

I tried with crypto/fs, both broken. You have to move it to a different file and import from there.

https://discord.com/channels/770287896669978684/771068344320786452/828673055777095720

@ryanflorence ryanflorence changed the title Can't import node builtins in a route module, I tried with crypto/fs, both broken. You have to move it to a different file and import from there. Can't import node builtins in a route module Apr 12, 2021
@mjackson mjackson transferred this issue from another repository May 4, 2021
@mjackson
Copy link
Member

mjackson commented May 4, 2021

esbuild issue: evanw/esbuild#705

@Haroenv
Copy link

Haroenv commented Nov 23, 2021

events also has this issue. I was using a package that uses the npm version of events, but somehow it still resolves to the node builtin in the browser (unless I alias it to something else, but it's hard to do in this case, as it's quite deep in the module tree)

Watching Remix app in development mode...
It appears you're using a module that is built in to node, but you installed it as a dependency which could cause problems. Please remove events before continuing.
💿 Built in 10ms

Is there some setting to resolve to the npm version if it's a dependency?

@Haroenv
Copy link

Haroenv commented Nov 23, 2021

When I use a package that transitively depends on events, I get this:

https://github.com/Haroenv/remix-bug-report/blob/487e5b242e5ee377165e77da80f526d00c95191b/app/routes/index.tsx#L3

Screenshot 2021-11-23 at 11 36 52

Dynamic import of "events" is not supported

I guess somehow it tries to import the builtin still?

@jtsiskin
Copy link

Same here:
image

within /public/build it tries to import events module:
image
image

@jtsiskin
Copy link

@mjackson are you able to fix this? You currently can't use any module which uses the 'events' in the browser in remix.
It needs to be bundled not external
For the simplest repo just var EventEmitter = require("events"); and console.log(EventEmitter)

https://github.com/remix-run/remix/blob/main/packages/remix-dev/compiler.ts#L310

image

@jtsiskin
Copy link

For anyone who needs a temporary workaround while this is fixed, here's my hack:
npm i patch-package
npm install @esbuild-plugins/node-modules-polyfill
npm install @esbuild-plugins/node-globals-polyfill

add this patch to patches/ : https://gist.github.com/jtsiskin/9f661fc35d5c4d1e91239f9b319693e0
npx patch-packages

Inside my root.tsx, within my , I then added
<script> var global = global || window; </script>

After than I had no more dependency issues

margalit added a commit to margalit/wagmi that referenced this issue Jan 15, 2022
margalit added a commit to margalit/wagmi that referenced this issue Jan 16, 2022
tmm pushed a commit to wevm/wagmi that referenced this issue Jan 16, 2022
* docs: create remix example

* docs: add dotenv to remix example

* docs: copy components into remix example

* docs: attach process to the window to prevent errors

* docs: re-enable walletConnect and walletLinkConnector

* docs: update components in remix example

* docs: remove global assignment

* docs: working metamask example

* docs: add patch to expose node global modules

remix-run/remix#114

* docs: working WalletConnect implementation

* docs: add safe-event-emitter patch

* docs: working walletlink example
@arthuryeti
Copy link

@jtsiskin thank you! I'll update your gist for remix 1.1.3

@afk-mario
Copy link

"@remix-run/dev": "^1.2.1" added support for builtin node-modules-polyfill but not for node-globals-polyfill

New workaround patch:

diff --git a/node_modules/@remix-run/dev/compiler.js b/node_modules/@remix-run/dev/compiler.js
index d36818e..5f6a45b 100644
--- a/node_modules/@remix-run/dev/compiler.js
+++ b/node_modules/@remix-run/dev/compiler.js
@@ -19,6 +19,7 @@ var fse = require('fs-extra');
 var debounce = require('lodash.debounce');
 var chokidar = require('chokidar');
 var nodeModulesPolyfill = require('@esbuild-plugins/node-modules-polyfill');
+var nodeGlobalsPolyfill = require('@esbuild-plugins/node-globals-polyfill');
 var build$1 = require('./build.js');
 var config = require('./config.js');
 var warnings = require('./warnings.js');
@@ -329,7 +330,7 @@ async function createBrowserBuild(config, options) {
       "process.env.NODE_ENV": JSON.stringify(options.mode),
       "process.env.REMIX_DEV_SERVER_WS_PORT": JSON.stringify(config.devServerPort)
     },
-    plugins: [mdx.mdxPlugin(config), browserRouteModulesPlugin.browserRouteModulesPlugin(config, /\?browser$/), emptyModulesPlugin.emptyModulesPlugin(config, /\.server(\.[jt]sx?)?$/), nodeModulesPolyfill.NodeModulesPolyfillPlugin()]
+    plugins: [mdx.mdxPlugin(config), browserRouteModulesPlugin.browserRouteModulesPlugin(config, /\?browser$/), emptyModulesPlugin.emptyModulesPlugin(config, /\.server(\.[jt]sx?)?$/), nodeModulesPolyfill.NodeModulesPolyfillPlugin(), nodeGlobalsPolyfill.NodeGlobalsPolyfillPlugin()]
   });
 }

@arthuryeti
Copy link

@afk-mario thank you ser

@chaance chaance closed this as completed Apr 19, 2022
neptune-web added a commit to neptune-web/react-hooks-ethereum that referenced this issue May 23, 2022
* docs: create remix example

* docs: add dotenv to remix example

* docs: copy components into remix example

* docs: attach process to the window to prevent errors

* docs: re-enable walletConnect and walletLinkConnector

* docs: update components in remix example

* docs: remove global assignment

* docs: working metamask example

* docs: add patch to expose node global modules

remix-run/remix#114

* docs: working WalletConnect implementation

* docs: add safe-event-emitter patch

* docs: working walletlink example
mattstern31 added a commit to mattstern31/react-wagmi that referenced this issue Feb 8, 2024
* docs: create remix example

* docs: add dotenv to remix example

* docs: copy components into remix example

* docs: attach process to the window to prevent errors

* docs: re-enable walletConnect and walletLinkConnector

* docs: update components in remix example

* docs: remove global assignment

* docs: working metamask example

* docs: add patch to expose node global modules

remix-run/remix#114

* docs: working WalletConnect implementation

* docs: add safe-event-emitter patch

* docs: working walletlink example
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

7 participants