process.env.NODE_ENV reference causes error "require is not defined" when bundling with webpack (for GraphiQL)
See original GitHub issueWhen bundling graphql-js with webpack, e.g. as dependency of graphiql, you get this error at runtime:
ReferenceError: require is not defined
at Module.../sourcegraph/node_modules/graphql/jsutils/instanceOf.mjs (instanceOf.mjs:28)
at __webpack_require__ (bootstrap:63)
at Module.../sourcegraph/node_modules/graphql/type/definition.mjs (definition.mjs:1)
at __webpack_require__ (bootstrap:63)
at Module.../sourcegraph/node_modules/graphql/type/validate.mjs (validate.mjs:1)
at __webpack_require__ (bootstrap:63)
at Module.../sourcegraph/node_modules/graphql/graphql.mjs (graphql.mjs:1)
at __webpack_require__ (bootstrap:63)
at Module.../sourcegraph/node_modules/graphql/index.mjs (index.mjs:2)
at __webpack_require__ (bootstrap:63)
This is because instanceOf.js contains a reference to process.env: https://github.com/graphql/graphql-js/blob/dec24f9/src/jsutils/instanceOf.js#L19-L36
In the ESM build, which is used by webpack by following the module field in package.json, the file gets compiled to a .mjs file, which does not get applied the webpack ProvidePlugin, see https://github.com/webpack/webpack/issues/7032.
This makes it impossible to bundle graphiql with webpack.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Bundle.js - Uncaught ReferenceError: process is not defined ...
To avoid error like denoted in the question I had have provide in webpack.config.js the next configuration (note defining variable level: process.env ):...
Read more >ReferenceError: “process is not defined” - GIMTEC
In NodeJS, “process” is defined, but not in the browser. This is because NodeJS ... This causes the error const { NODE_ENV }...
Read more >Handling process.env - Quasar Framework
You might be getting process is not defined errors in the browser console if you are accessing the variables wrong or if you...
Read more >TSConfig Reference - Docs on every TSConfig option
Lets you set a base directory to resolve non-absolute module names. You can define a root folder where you can do absolute file...
Read more >Environment Variables - Razzle
This path is used by react-error-overlay and webpack to power up the fancy runtime error ... Note: This does not impact production bundling....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@xialvjun You use
0.13.2, please updategraphqlto14.0.2I understand that you mean my webpack config. But there are many rules defined in there (for loaders), and none of them seem related to providing
process.env. I already include.mjsin the regex for my loader rules.