question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react-scripts@2.0.0-next.3e165448 - ReferenceError: require is not defined :: due to graphql using .mjs

See original GitHub issue

Is this a bug report?

Yes.

The latest react-scripts@2.0.0-next.3e165448 with Webpack 4 support breaks when bundling dependencies (node_modules) with .mjs files, such as graphql.

Did you try recovering your dependencies?

Yes.

Which terms did you search for in User Guide?

In the User Guide, Issues and PRs I searched for:

  • ReferenceError: require is not defined
  • graphql
  • mjs

Environment

Environment:
  OS:  macOS Sierra 10.12.6
  Node:  8.9.4
  Yarn:  1.7.0
  npm:  5.6.0
  Watchman:  4.1.0
  Xcode:  Xcode 9.2 Build version 9C40b
  Android Studio:  2.1 AI-143.3101438

Packages: (wanted => installed)
  react: ^16.4.1 => 16.4.1
  react-dom: ^16.4.1 => 16.4.1
  react-scripts: ^2.0.0-next.3e165448 => 2.0.0-next.3e165448

Steps to Reproduce

(Write your steps here:)

  1. npx create-react-app my-app && cd my-app
  2. npm install
  3. npm install graphql react-scripts@2.0.0-next.3e165448 --save
  4. Add [graphqlCode] after import "./App.css" in src/App.js
  5. npm start

[graphqlCode]

import {
  graphql,
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString
} from "graphql";

var schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: "RootQueryType",
    fields: {
      hello: {
        type: GraphQLString,
        resolve() {
          return "world";
        }
      }
    }
  })
});

Expected Behavior

create react app example

Actual Behavior

ReferenceError: require is not defined

Reproducible Demo

https://github.com/christemple/create-react-app-graphql-issue

git clone git@github.com:christemple/create-react-app-graphql-issue.git
npm install
npm start

Additional

I noticed this when trying to install the AWS Amplify package, which I noticed has a dependency on graphql.

I’m happy to raise a PR for a fix, I just wanted to get verification that it’s a needed fix before doing so.

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
christemplecommented, Jun 18, 2018

The following in the webpack config, just before the file loader, appears to solve the issue:

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

I can do more testing and get a root cause and solution along with a PR up this evening

0reactions
bebbicommented, Nov 20, 2018

Because my api uses different babel versions, I had react-scripts in the monorepo’s nohoist declaration. I was then able to resolve this by adding further libs to the nohoist declaration:

"nohoist": [
  "**/react-scripts",
  "**/react-scripts/**",
  "**/graphql",
  "**/graphql/**",
  "**/apollo",
  "**/apollo/**",
  "**/apollo-link-state",
  "**/apollo-link-state/**",
  "**/babel-loader",
  "**/babel-loader/**"
]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Client on Node.js: Uncaught ReferenceError: require is not ...
throws the Uncaught ReferenceError: require is not defined. I was able to work around that by specifying Node.js integration as true when ...
Read more >
Getting Started With GraphQL.js
To handle GraphQL queries, we need a schema that defines the Query type, and we need an API root with a function called...
Read more >
API Reference: ApolloServer - Apollo GraphQL Docs
This article documents the ApolloServer class from the @apollo/server package. You can use the ApolloServer class to create an instance of Apollo Server ......
Read more >
TypeGraphQL · Modern framework for GraphQL API in Node.js
This allows you to define both the GraphQL type and the entity in a single class - no need to jump between multiple...
Read more >
graphql-request - npm
import { request, GraphQLClient } from 'graphql-request' // Run GraphQL queries/mutations using a static function request(endpoint, query, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found