Create-react-app: [v4]ReferenceError: React is not defined

Created on 24 Oct 2020  路  3Comments  路  Source: facebook/create-react-app

npx create-react-app my-app

then eject it, it report React is not defined

鎴睆2020-10-24 11 57 16

bug report needs triage

Most helpful comment

I have submitted PR #9885 to fix this.

This is because the jsx-runtime configuration gets removed webpack config during the ejection process.

In the meantime, Adding a runtime option to babel in package.json will solve the issue

// package.json
{
  "babel": {
    "presets": [
      [
        "react-app",
        {
          "runtime": "automatic"
        }
      ]
    ]
  }
}

or add presents option back to webpack.config.js (around line 409)

// webpack.config.js
{
  loader: require.resolve('babel-loader'),
  options: {
    customize: require.resolve(
      'babel-preset-react-app/webpack-overrides'
    ),
    // Add this back
    presets: [
      [
        require.resolve('babel-preset-react-app'),
        {
          runtime: hasJsxRuntime ? 'automatic' : 'classic',
        },
      ],
    ],
  }
}

All 3 comments

I have submitted PR #9885 to fix this.

This is because the jsx-runtime configuration gets removed webpack config during the ejection process.

In the meantime, Adding a runtime option to babel in package.json will solve the issue

// package.json
{
  "babel": {
    "presets": [
      [
        "react-app",
        {
          "runtime": "automatic"
        }
      ]
    ]
  }
}

or add presents option back to webpack.config.js (around line 409)

// webpack.config.js
{
  loader: require.resolve('babel-loader'),
  options: {
    customize: require.resolve(
      'babel-preset-react-app/webpack-overrides'
    ),
    // Add this back
    presets: [
      [
        require.resolve('babel-preset-react-app'),
        {
          runtime: hasJsxRuntime ? 'automatic' : 'classic',
        },
      ],
    ],
  }
}

Thank you @n3tr! Changing the webpack config as you suggested worked for me.

Thanks, only package.json solution worked for me.
Edit: This didn't fully work as intended.
After trying a few different things, what worked for me was:

  1. create a new react app project.
  2. Implement @n3tr changes given by his pull request(It's only moving 2 lines of code).
  3. Finally ejecting.

I then just copied my source files over.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jnachtigall picture jnachtigall  路  3Comments

stopachka picture stopachka  路  3Comments

xgqfrms-GitHub picture xgqfrms-GitHub  路  3Comments

onelson picture onelson  路  3Comments

rdamian3 picture rdamian3  路  3Comments