Graphql-js: process.env.NODE_ENV reference causes error "require is not defined" when bundling with webpack (for GraphiQL)

Created on 28 Sep 2018  路  7Comments  路  Source: graphql/graphql-js

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 https://github.com/webpack/webpack/issues/7032.

This makes it impossible to bundle graphiql with webpack.

Most helpful comment

@xialvjun You use 0.13.2, please update graphql to 14.0.2

All 7 comments

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

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

@jgcmarins to what rule?

on your webpack.config.js

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.

same error

here is my yarn.lock and package.json gist

@xialvjun You use 0.13.2, please update graphql to 14.0.2

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adriano-di-giovanni picture adriano-di-giovanni  路  3Comments

benjie picture benjie  路  4Comments

scf4 picture scf4  路  3Comments

gjuchault picture gjuchault  路  4Comments

itajaja picture itajaja  路  3Comments