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

Runtime Error "require is not defined" when bundling with webpack #721

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

Comments

@felixfbecker
Copy link

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

This makes it impossible to bundle graphiql with webpack.

Also reported to graphql-js at graphql/graphql-js#1536, but filing here for visibility.

@amcdnl
Copy link

amcdnl commented Oct 3, 2018

Getting the same.... :(

@hisapy
Copy link

hisapy commented Oct 8, 2018

Me too

@tomeightyeight
Copy link

tomeightyeight commented Oct 18, 2018

Fixed this issue by explicitly specifying latest release of graphql dependency:

https://github.com/graphql/graphql-js/

npm install --save graphql@^14.0.2

morrys added a commit to morrys/aws-mobile-appsync-sdk-js that referenced this issue Nov 25, 2018
…browserCryptoLib', upgrade version graphql for error: ReferenceError: require is not defined graphql/graphiql#721, TODO: jest error Emit Skipped, Not Tested: IAM autentication and complex object
Esadruhn added a commit to Esadruhn/dd5-monsters that referenced this issue Jan 10, 2019
julienc91 added a commit to julienc91/lionskins that referenced this issue Feb 4, 2019
@airandfingers
Copy link

I had this error with another project with graphql as a dependency, and adding this to my Webpack rules fixed it for me:

{
  test: /\.mjs$/,
  include: /node_modules/,
  type: "javascript/auto",
}

As Stereobit explained, "Graphql-js uses .mjs as file extension which caused issues with the webpack build."

@acao
Copy link
Member

acao commented Dec 2, 2019

we have a webpack example now, 0.17.0 should build just fine! just be sure to follow the example webpack and babel config.

@acao acao closed this as completed Dec 2, 2019
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

6 participants