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

process.env.NODE_ENV reference causes error "require is not defined" when bundling with webpack (for GraphiQL) #1536

Closed
felixfbecker opened this issue Sep 28, 2018 · 7 comments

Comments

@felixfbecker
Copy link

When 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 webpack/webpack#7032.

This makes it impossible to bundle graphiql with webpack.

@jgcmarins
Copy link

I think you can solve that just adding more extensions to your webpack rules:

test: /\.js?$/ -> test: /\.(js|mjs)?$/

@felixfbecker
Copy link
Author

@jgcmarins to what rule?

@jgcmarins
Copy link

on your webpack.config.js

@felixfbecker
Copy link
Author

I 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 .mjs in the regex for my loader rules.

@xialvjun
Copy link

xialvjun commented Nov 23, 2018

same error

here is my yarn.lock and package.json gist

@IvanGoncharov
Copy link
Member

@xialvjun You use 0.13.2, please update graphql to 14.0.2

@IvanGoncharov
Copy link
Member

Should be fixed in 14.0.0.
Feel free to open a new issue if you still experience this problem after 14.0.0 update.

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

4 participants