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

[0.57.0-rc.4] Running local-cli commands fails with "regeneratorRuntime is not defined" when using babel.config.js #21052

Closed
3 tasks done
jonathanglasmeyer opened this issue Sep 11, 2018 · 18 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@jonathanglasmeyer
Copy link

Environment

  React Native Environment Info:
    System:
      OS: macOS High Sierra 10.13.3
      CPU: x64 Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
      Memory: 1.90 GB / 16.00 GB
      Shell: 5.3 - /bin/zsh
    Binaries:
      Node: 8.9.4 - ~/.nvm/versions/node/v8.9.4/bin/node
      Yarn: 1.5.1 - /usr/local/bin/yarn
      npm: 5.6.0 - ~/.nvm/versions/node/v8.9.4/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 11.4, macOS 10.13, tvOS 11.4, watchOS 4.3
    IDEs:
      Xcode: 9.4.1/9F2000 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.4.1 => 16.4.1
      react-native: 0.57.0-rc.4 => 0.57.0-rc.4
    npmGlobalPackages:
      react-native-cli: 2.0.1

Description

Using react-native 0.57.0-rc.4, the following issue exists:
Running the react-native local-cli (e.g. running the npm start command of a fresh react-native project) fails with

ReferenceError: regeneratorRuntime is not defined
    at getCliConfig (/Users/jwerner/projects/rn-test/Foo/node_modules/react-native/local-cli/core/index.js:128:44)

when babel is setup with babel.config.js:

// babel.config.js
module.exports = {
  "presets": ["module:metro-react-native-babel-preset"]
}

Everything works when the same babel configuration is applied in the old .babelrc format.

I traced down the problem by inserting console.logs in node_modules/@babel/register/lib/node.js, function compile. When using babel.config.js you can see that all plugins of module:metro-react-native-babel-preset are being applied to the node_modules/react-native/local-cli/core/index.js file; when using .babelrc that's not the case. The code breaks because apparently regeneratorRuntime call is inserted, but babel/polyfill (or regenerator-runtime/runtime) import is not happening.

Is this behaviour known? How are users expected to make use of babel.config.js?

Reproducible Demo

Repository with working state
With a broken state

Repro:

  • clone repo
  • checkout commit ee4497c2e2856ad6c52d632c73ad0e038d5b7d03
  • npm install / yarn install
  • npm start
  • see how the error above is reproduced
@bob76828
Copy link

I had the same problem before.
My solution is adding @babel/plugin-transform-runtime into babel plugin list.

@Mokto
Copy link

Mokto commented Sep 13, 2018

Awesome ! Thanks.

@Piterden
Copy link

Not work for me

{
  "presets": [
    [
      "@babel/env",
      {
        "modules": false,
        "targets": {
          "browsers": [
            ">0.25%",
            "not ie 11",
            "not op_mini all"
          ]
        }
      }
    ]
  ],
  "plugins": [
    "@babel/proposal-class-properties",
    "@babel/proposal-export-namespace-from",
    "@babel/proposal-function-sent",
    "@babel/proposal-json-strings",
    "@babel/proposal-numeric-separator",
    "@babel/proposal-throw-expressions",
    "@babel/syntax-dynamic-import",
    "@babel/syntax-import-meta",
    "@babel/transform-runtime"
  ]
}

@fhadsheikh
Copy link

@Piterden Try @babel/plugin-transform-runtime in your plugins array

@Piterden
Copy link

{
  "presets": [
    [
      "@babel/env",
      {
        "modules": false,
        "useBuiltIns": "usage",
        "targets": {
          "browsers": ["last 2 Chrome versions"]
        },
        "debug":true
      }
    ]
  ],
  "plugins": [
    "@babel/transform-async-to-generator",
    "@babel/transform-runtime"
  ]
}

The same error

@Piterden
Copy link

@Piterden Try @babel/plugin-transform-runtime in your plugins array

Sorry, it is included.

@helenaford
Copy link
Contributor

   "@babel/core": "7.0.0-beta.47",
    "@babel/plugin-proposal-decorators": "7.0.0-beta.47",
    "@babel/plugin-proposal-export-default-from": "7.0.0-beta.47",
    "@babel/plugin-transform-react-jsx-source": "7.0.0-beta.47",
    "@babel/plugin-transform-runtime": "7.0.0-beta.47",
    "@babel/runtime": "^7.0.0-beta.47", << as soon as I added this, plugin-transform-runtime worked. 

Without downgrading the runtime I had the following error after adding "plugin-transform-runtime":
"Cannot find module '@babel/runtime/helpers/interopRequireDefault'."

I got the hint to downgrade the runtime from meteor/meteor#10126

Also, because I downgraded the runtime I had to downgrade my other babel dependencies back to beta.47 too. My project uses react-native 56 and Metro 0.38.

I only added plugin-transform-runtime because of this bug. Hope it helps.

@Piterden
Copy link

Piterden commented Sep 15, 2018

Hey, Helena, thank you for response. But shouldn't "@babel/runtime" be in dependencies, not devDependencies?

Honestly I use Vue. But this bug is not depend on our frameworks. It is babel. I already wrote there an issue.

@helenaford
Copy link
Contributor

Hey, Piterden, no problem. I was just relieved there were people commenting on this issue and you were keeping the thread alive. But, yeah, I just noticed that so will move it before I commit. After this, I've opened the app and I've ran into another bug, and found this git issue.

"found that was caused by corejs option @babel/plugin-transform-runtime, I replaced it by @babel/polyfill"

#21048 (comment).

I'm thinking about resorting back to using .bablerc because it all worked except for being able to have separate environments.

@Piterden
Copy link

For me it not work, I spent 15 hrs )))

@helenaford
Copy link
Contributor

helenaford commented Sep 16, 2018

@fhadsheikh solution worked after I did the following:

 "dependencies": {
    "@babel/runtime": "7.0.0" <-- added this
    "react": "16.5.0",
    "react-native": "0.57.0",
 },

"devDependencies": {
      '@babel/plugin-transform-runtime', <-- add if you are using babel.config.js and not .babelrc
    "@babel/plugin-proposal-decorators": "^7.0.0",
    "@babel/plugin-proposal-export-default-from": "^7.0.0",
    "babel-eslint": "^8.2.6",
    "babel-plugin-transform-remove-console": "^6.9.4",
    "babel-preset-react-native": "5.0.0",
    "metro-react-native-babel-preset": "0.45.3",
    "react-devtools": "^3.3.2",
    "react-native-dotenv": "^0.2.0",
  },

It helped to create a new project with "react-native init" and fix the bug to see which dependencies were needed.

N.B. No need for @babel/plugin-transform-runtime if using .babelrc.

My babel config is:

presets: [
      'module:metro-react-native-babel-preset', 
      'module:react-native-dotenv',
    ],
    plugins: [
      '@babel/plugin-proposal-export-default-from',
      ['@babel/plugin-proposal-decorators', { legacy: true }],
      '@babel/plugin-transform-runtime', <- - add this if using babel.config.js and not .babelrc
    ],

@Piterden
Copy link

My solution is making those two methods sync, because new Mnemonic() inside async function anyway causes regenerator exception.

@Mentioum
Copy link

This was an absolute nightmare to work out - Thanks for your hard work @helenaford - saved me a bunch of time 👍

ide added a commit to ide/metro-bundler that referenced this issue Oct 1, 2018
…abelrc

This adds support for projects using `metro-babel-register` with Babel 7's babel.config.js files. The `metro-babel-register` package is designed to apply a static, hardcoded list of Babel plugins. It intentionally ignores .babelrc files by setting `babelrc: false`.

Babel 7 added a new configuration file called babel.config.js, with similar but slightly different loading semantics. To disable babel.config.js lookup, there is a new option called `configFile` instead of `babelrc`. See https://babeljs.io/docs/en/next/config-files#project-wide-configuration.

Fixes facebook/react-native#21052

Test plan: In a React Native project with babel.config.js and an error about `regeneratorRuntime` missing, said error goes away with this `metro-babel-register` patch.
@ospfranco
Copy link
Contributor

Thanks for the solution @helenaford ! saved my day!

@timxor
Copy link

timxor commented Nov 6, 2018

This is a really bad developer experience and why I stray from da fe 🧶

@kormic
Copy link

kormic commented Nov 8, 2018

How is this not included in the readme or in troubleshooting or something? I 've followed the get started guide to run a hello world app and I 've struggled for two days searching for this. I ended up using react-native init instead of expo init and then run react-native run-ios twice till I get the project running.

@slavik0329
Copy link

I had to also install the following plugins and dependancies that @helenaford 's comment shows:

'@babel/plugin-proposal-export-default-from',
['@babel/plugin-proposal-decorators', { legacy: true }],

@leovazquezz1
Copy link

I was having this problem when I runned this command "yarn". I fixed it by running "npm install" after I deleted my node_modules folder. If it does not work with yarn try npm

@facebook facebook locked as resolved and limited conversation to collaborators Oct 2, 2019
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Oct 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests