Skip to content

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

Closed
@felixfbecker

Description

@felixfbecker

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions