Runtime Error "require is not defined" when bundling with webpack
See original GitHub issueWhen bundling graphiql with webpack, 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
in graphql
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.
Also reported to graphql-js
at https://github.com/graphql/graphql-js/issues/1536, but filing here for visibility.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:17
- Comments:5 (1 by maintainers)
Top Results From Across the Web
webpack : Uncaught ReferenceError: require is not defined
I encountered this problem a few minutes ago. This usually happens when you mix up the target property for the bundle in webpack.config...
Read more >How to fix the error `require is not defined` in Node.js
Today I started writing a new project on Node.js and got this error on the very first run: const express = require('express'); ...
Read more >How To Fix ReferenceError require is not defined in JavaScript
In this case, check your package.json file for an property called type . If that is set to module , ES6 modules will...
Read more >To v5 from v4 - webpack
But we do list some common advice below regarding Runtime Errors: process is not defined. webpack 5 does no longer include a polyfill...
Read more >require is not defined babel | The AI Search Engine You Control
Here is a minimum configuration to get you started. Issue npm run start to bring up webpack-dev-server at port 9000. babel-core was missing...
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 FreeTop 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
Top GitHub Comments
Fixed this issue by explicitly specifying latest release of graphql dependency:
https://github.com/graphql/graphql-js/
Getting the same… 😦