20

I built react project with webpack and babel.

It worked well.

But, today I got some error below.

ERROR in ./index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: [BABEL] /home/rpf5573/react-discovery-v2/src/admin/admin-client/index.js: Invalid Option: corejs is not a valid top-level option.
        Maybe you meant to use 'targets'? (While processing: "/home/rpf5573/react-discovery-v2/src/admin/admin-client/node_modules/@babel/preset-env/lib/index.js")
    at validateTopLevelOptions (/home/rpf5573/react-discovery-v2/src/admin/admin-client/node_modules/@babel/preset-env/lib/normalize-options.js:49:13)
    at normalizeOptions (/home/rpf5573/react-discovery-v2/src/admin/admin-client/node_modules/@babel/preset-env/lib/normalize-options.js:160:3)
    at _default (/home/rpf5573/react-discovery-v2/src/admin/admin-client/node_modules/@babel/preset-env/lib/index.js:168:37)
  ....
error Command failed with exit code 2.

And this is my admin-client/.babelrc

module.exports = {
  compact: true,
  presets: [
    [
      "@babel/preset-env",
      {
        modules: false,
        targets: {
          browsers: ["since 2015"]
        },
        useBuiltIns: "usage",
        corejs: "2"
      }
    ],
    "@babel/preset-react"
  ],
  plugins: [
    "@babel/plugin-proposal-class-properties"
  ]
}

What did I wrong ?

What should I do ?

4
  • Tried deleting node_modules? Apr 15, 2019 at 8:50
  • 1
    @DennisVash nope, I just deleted corejs: "2" line from .babelrc. And now works well. Apr 16, 2019 at 7:32
  • 1
    Also deleting your npm or yarn lock file and regenerating can help.
    – TrueWill
    May 18, 2019 at 19:27
  • Not sure if that could be the case but for sure core3 goes in conflict with core2, could be that in your case some package is already looking for core3 thebasement.be/updating-to-babel-7.4 Aug 31, 2019 at 12:45

3 Answers 3

28

Maybe you had some major version change recently?

In my case, that was the cause. And the solution was:

  • Completely remove node-modules folder from the project

  • Remove package-lock.json file from the project root folder

  • Perform a npm i to recreate everything

0
3

I experienced the same challenge

As an alternative to entirely deleting node-modules as suggested by @SysDragon. I looked deeper into why the complaint was specific to corejs. And i found a great remedy here.

In summary, these were the steps taken:

  • Install the latest version of core-js (or atleast version-3). Whatever you are building "might" be making use of latest JavaScript functionality. For my case, i was working with react-16.9.0 and mdbreact-4.19.0.

Note: Core-js is a Modular standard library for JavaScript. Includes polyfills for ECMAScript up to 2019: promises, symbols, collections, iterators, typed arrays and many other features.

  • Then, you need to update your transpiler, babel and it's "relatives". This block snippet helped accomplish that: yarn upgrade @babel/core @babel/plugin-transform-runtime @babel/polyfill @babel/preset-env @babel/runtime babel-loader --dev.

The --dev part is just to save them as devDependencies (if using yarn) (for npm, use --save-dev), which i advocate for especially if this is team project.

1
  • 1
    I just needed to install core-js. Thank you very much! o/
    – diogo
    Nov 1, 2019 at 16:49
1

In my case, the subdirectory I was working on was part of a larger Yarn workspace in the root of the repository and removing that subdirectory from the workspace and reinstalling node_modules within the subdirectory fixed the issue.

This issue is most likely caused by dependency mismatches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.