process is not defined when using a package that depends on it (in client) #4906
-
Read me!I fully understand that from several other issues mentioned here: We should not be using process.env in the client. However, this issue is not concerning that. It's concerning the fact that other packages might be using it. Wanted to start a discussion on this. What version of Remix are you using?1.8.2 Steps to Reproduce
Expected BehaviorI expect us to be able to pull in this package Actual Behavior
This is because the client dependency
Is there any way I can globally inject this fake variable into remix's client environment? I anticipate that other packages that utilize a reference to |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 10 replies
-
This is also happening to me in another context. We have a monorepo and we're importing some local packages that are used on the client side, these libs like a UI component library or shared configs are using the process.env behind-the-scenes reading variables injected for the whole monorepo, this is working for other apps we have created using other frameworks like Nextjs or simple CRA apps and changing the way Envs are used in the shared libs would break other existing apps, so this is similar to what @chang-ryan mentions, is there any way to solve this? |
Beta Was this translation helpful? Give feedback.
-
You can use an inline script to define |
Beta Was this translation helpful? Give feedback.
-
I was able to solve issues with libs (web3auth, etc) using process.env and Buffer from Browser with the below patch:
diff --git a/node_modules/@remix-run/dev/dist/compiler/compileBrowser.js b/node_modules/@remix-run/dev/dist/compiler/compileBrowser.js
index 1fa83a5..5892640 100644
--- a/node_modules/@remix-run/dev/dist/compiler/compileBrowser.js
+++ b/node_modules/@remix-run/dev/dist/compiler/compileBrowser.js
@@ -16,11 +16,11 @@ var path = require('path');
var module$1 = require('module');
var esbuild = require('esbuild');
var nodeModulesPolyfill = require('@esbuild-plugins/node-modules-polyfill');
+var nodeGlobalsPolyfill = require('@esbuild-plugins/node-globals-polyfill');
var assets = require('./assets.js');
var dependencies = require('./dependencies.js');
var loaders = require('./loaders.js');
var browserRouteModulesPlugin = require('./plugins/browserRouteModulesPlugin.js');
-var cssFilePlugin = require('./plugins/cssFilePlugin.js');
var deprecatedRemixPackagePlugin = require('./plugins/deprecatedRemixPackagePlugin.js');
var emptyModulesPlugin = require('./plugins/emptyModulesPlugin.js');
var mdx = require('./plugins/mdx.js');
@@ -77,10 +77,18 @@ const createEsbuildConfig = (config, options) => {
// that we don't want to run in the browser (i.e. action & loader).
entryPoints[id] = config.routes[id].file + "?browser";
}
- let plugins = [deprecatedRemixPackagePlugin.deprecatedRemixPackagePlugin(options.onWarning), cssFilePlugin.cssFilePlugin({
- mode: options.mode,
- rootDirectory: config.rootDirectory
- }), urlImportsPlugin.urlImportsPlugin(), mdx.mdxPlugin(config), browserRouteModulesPlugin.browserRouteModulesPlugin(config, /\?browser$/), emptyModulesPlugin.emptyModulesPlugin(config, /\.server(\.[jt]sx?)?$/), nodeModulesPolyfill.NodeModulesPolyfillPlugin()];
+ let plugins = [
+ nodeGlobalsPolyfill.NodeGlobalsPolyfillPlugin({
+ buffer: true,
+ process: true
+ }),
+ nodeModulesPolyfill.NodeModulesPolyfillPlugin(),
+ deprecatedRemixPackagePlugin.deprecatedRemixPackagePlugin(options.onWarning),
+ urlImportsPlugin.urlImportsPlugin(),
+ mdx.mdxPlugin(config),
+ browserRouteModulesPlugin.browserRouteModulesPlugin(config, /\?browser$/),
+ emptyModulesPlugin.emptyModulesPlugin(config, /\.server(\.[jt]sx?)?$/)
+ ];
return {
entryPoints,
outdir: config.assetsBuildDirectory,
diff --git a/node_modules/@remix-run/dev/dist/compiler/compilerServer.js b/node_modules/@remix-run/dev/dist/compiler/compilerServer.js
index 21cbad7..d70b0e3 100644
--- a/node_modules/@remix-run/dev/dist/compiler/compilerServer.js
+++ b/node_modules/@remix-run/dev/dist/compiler/compilerServer.js
@@ -16,8 +16,8 @@ var path = require('path');
var esbuild = require('esbuild');
var fse = require('fs-extra');
var nodeModulesPolyfill = require('@esbuild-plugins/node-modules-polyfill');
+var nodeGlobalsPolyfill = require('@esbuild-plugins/node-globals-polyfill');
var loaders = require('./loaders.js');
-var cssFilePlugin = require('./plugins/cssFilePlugin.js');
var deprecatedRemixPackagePlugin = require('./plugins/deprecatedRemixPackagePlugin.js');
var emptyModulesPlugin = require('./plugins/emptyModulesPlugin.js');
var mdx = require('./plugins/mdx.js');
@@ -63,11 +63,14 @@ const createEsbuildConfig = (config, assetsManifestChannel, options) => {
}
let isCloudflareRuntime = ["cloudflare-pages", "cloudflare-workers"].includes(config.serverBuildTarget ?? "");
let isDenoRuntime = config.serverBuildTarget === "deno";
- let plugins = [deprecatedRemixPackagePlugin.deprecatedRemixPackagePlugin(options.onWarning), cssFilePlugin.cssFilePlugin({
- mode: options.mode,
- rootDirectory: config.rootDirectory
- }), urlImportsPlugin.urlImportsPlugin(), mdx.mdxPlugin(config), emptyModulesPlugin.emptyModulesPlugin(config, /\.client(\.[jt]sx?)?$/), serverRouteModulesPlugin.serverRouteModulesPlugin(config), serverEntryModulePlugin.serverEntryModulePlugin(config), serverAssetsManifestPlugin.serverAssetsManifestPlugin(assetsManifestChannel.read()), serverBareModulesPlugin.serverBareModulesPlugin(config, options.onWarning)];
+ let plugins = [deprecatedRemixPackagePlugin.deprecatedRemixPackagePlugin(options.onWarning), urlImportsPlugin.urlImportsPlugin(), mdx.mdxPlugin(config), emptyModulesPlugin.emptyModulesPlugin(config, /\.client(\.[jt]sx?)?$/), serverRouteModulesPlugin.serverRouteModulesPlugin(config), serverEntryModulePlugin.serverEntryModulePlugin(config), serverAssetsManifestPlugin.serverAssetsManifestPlugin(assetsManifestChannel.read()), serverBareModulesPlugin.serverBareModulesPlugin(config, options.onWarning)];
if (config.serverPlatform !== "node") {
+ plugins.unshift(
+ nodeGlobalsPolyfill.NodeGlobalsPolyfillPlugin({
+ buffer: true,
+ process: true
+ })
+ );
plugins.unshift(nodeModulesPolyfill.NodeModulesPolyfillPlugin());
}
return { And from package.json: "scripts": {
"postinstall": "run-s postinstall:*",
"postinstall:patch": "patch-package",
"postinstall:remix": "remix setup node",
}, Let me know if there's any solution to avoid disabling cssFilePlugin! |
Beta Was this translation helpful? Give feedback.
-
There is this package https://www.npmjs.com/package/@storyblok/react that you must init inside the root.tsx and outside of anything
So for me, none of the solutions listed in here works https://remix.run/docs/en/1.14.0/guides/envvars#browser-environment-variables |
Beta Was this translation helpful? Give feedback.
-
I'm facing the same issue. I need Shopify Admin API to run on a hydrogen app (using remix). But this admin API requires access to the |
Beta Was this translation helpful? Give feedback.
-
Hey folks, check my example to fix this issue https://gist.github.com/jdnichollsc/b73eca1f34bdd1e3a95283774fda2212 |
Beta Was this translation helpful? Give feedback.
-
In my case, I was importing utils from outside the app directory. It turns out undesired code ended up in the client bundle because remix/packages/remix-dev/compiler/plugins/emptyModules.ts Lines 19 to 27 in c9648c7 |
Beta Was this translation helpful? Give feedback.
-
Any updates here? @chang-ryan @sergiodxa @jdnichollsc @anakinsolo @buildtheui |
Beta Was this translation helpful? Give feedback.
You can use an inline script to define
process.env
, see https://sergiodxa.com/articles/use-process-env-client-side-with-remix