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

WebpackError: ReferenceError: React is not defined #27694

Closed
mikaelkristiansson opened this issue Oct 28, 2020 · 3 comments
Closed

WebpackError: ReferenceError: React is not defined #27694

mikaelkristiansson opened this issue Oct 28, 2020 · 3 comments
Labels
type: bug An issue or pull request relating to a bug in Gatsby

Comments

@mikaelkristiansson
Copy link
Contributor

Description

Since update of gatsby version ^2.24.86 the error React is not defined started to occur when following the new jsx transformer setup: https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#gatsby

Here is the changes since the latest working version:
https://github.com/gatsbyjs/gatsby/compare/[email protected]

Steps to reproduce

Just run a gatsby app without import React from 'react'; in any files and run any version above 2.24.85

Expected result

should work with the new jsx transform

Actual result

WebpackError: ReferenceError: React is not defined

Environment

  System:
    OS: macOS 10.15.7
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.7.0 - ~/.nvm/versions/node/v14.7.0/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 6.14.7 - ~/.nvm/versions/node/v14.7.0/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  Browsers:
    Chrome: 86.0.4240.111
    Firefox: 81.0.2
    Safari: 14.0
  npmPackages:
    gatsby: 2.24.86 => 2.24.86
    gatsby-i18n: ^1.1.8 => 1.1.8
    gatsby-image: ^2.4.21 => 2.4.21
    gatsby-plugin-alias-imports: ^1.0.5 => 1.0.5
    gatsby-plugin-google-tagmanager: ^2.3.16 => 2.3.16
    gatsby-plugin-manifest: ^2.4.36 => 2.4.36
    gatsby-plugin-polyfill-io: ^1.1.0 => 1.1.0
    gatsby-plugin-react-helmet: ^3.3.14 => 3.3.14
    gatsby-plugin-react-svg: ^3.0.0 => 3.0.0
    gatsby-plugin-remove-trailing-slashes: ^2.3.13 => 2.3.13
    gatsby-plugin-sass: ^2.3.21 => 2.3.21
    gatsby-plugin-sharp: ^2.6.43 => 2.6.43
    gatsby-plugin-sitemap: ^2.4.17 => 2.4.17
    gatsby-source-contentful: ^3.0.5 => 3.0.5
    gatsby-transformer-sharp: ^2.5.20 => 2.5.20
@mikaelkristiansson mikaelkristiansson added the type: bug An issue or pull request relating to a bug in Gatsby label Oct 28, 2020
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Oct 28, 2020
@mikaelkristiansson
Copy link
Contributor Author

not sure if this could be it, but I noticed a new rollup config was created in gatsby-cli package and it has set the plugin @babel/preset-react but I think maybe it needs to be:

["@babel/preset-react", {
      "runtime": "automatic"
}]

https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-cli/rollup.config.js#L60

Currently, the old transform ("runtime": "classic") is the default option. To enable the new transform, you can pass {"runtime": "automatic"} as an option

@mikaelkristiansson
Copy link
Contributor Author

I created a custom babel.config.js and set it like this:

module.exports = api => {
    const isTest = api.env('test');

    return {
        presets: [
            [
                '@babel/env',
                {
                    // use ES modules for rollup and commonjs for jest
                    modules: isTest ? `commonjs` : false,
                    shippedProposals: true,
                    targets: {
                        node: '10.13.0',
                    },
                },
            ],
            [
                '@babel/preset-react',
                {
                    runtime: 'automatic',
                },
            ],
        ],
        plugins: ['@babel/plugin-transform-runtime'],
    };
};

and this worked, so the problem is the setup with @babel/preset-react.

@mikaelkristiansson
Copy link
Contributor Author

ok so I found that you disabled the feature: #27615
maybe this info should be presented somewhere since according to react this should be working by default?

@LekoArts LekoArts removed the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Oct 28, 2020
samhwang added a commit to samhwang/gatsby-site-archived that referenced this issue Nov 1, 2020
See these URLs: apparently Gatsby ecosystem has some errors that does
not support the new JSX runtime by default yet.

gatsbyjs/gatsby#27694
https://github.com/gatsbyjs/gatsby/pull/27615/files
samhwang added a commit to samhwang/gatsby-site-archived that referenced this issue Nov 1, 2020
See these URLs: apparently Gatsby ecosystem has some errors that does
not support the new JSX runtime by default yet.

gatsbyjs/gatsby#27694
https://github.com/gatsbyjs/gatsby/pull/27615/files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

No branches or pull requests

2 participants