webpack-cli: Unable to use webpack-cli with @babel/core and babel-loader "^7.1.3"

Unable to use webpack-cli with @babel/core and babel-loader "^7.1.3" and since webpack-cli uses jscodeshift which intern uses babel-register "^6.9.0" and babel-core "^6.26.0" which is causing the following issue when used along with @babel/core "^7.0.0-beta.40"

ISSUE: Module build failed: TypeError: Cannot read property 'loose' of undefined (While processing preset: "/node_modules/@babel/preset-env/lib/index.js")

Issue has been raised in the @babel/core but according this (https://github.com/babel/babel/issues/7110#issuecomment-354583771) it might be due to use of babel version 6 by some dependencies

using the following config

// package.json

"dependencies": {
    "react": "^16.2.0",
    "react-dom": "^16.2.0"
  },
  "devDependencies": {
    "@babel/core": "^7.0.0-beta.40",
    "@babel/preset-env": "^7.0.0-beta.40",
    "@babel/preset-flow": "^7.0.0-beta.40",
    "@babel/preset-react": "^7.0.0-beta.40",
    "babel-loader": "^7.1.3",
    "html-webpack-plugin": "webpack-contrib/html-webpack-plugin",
    "webpack": "^4.0.1",
    "webpack-cli": "^2.0.9"
  },
  "babel": {
    "presets": [
      "@babel/preset-env",
      {
        "targets": {
          "browsers": [
            "last 2 versions",
            "safari >= 7"
          ]
        }
      }
    ]
  }
// webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

// plugins
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
  template: './src/index.html',
  filename: 'index.html',
  inject: 'body',
});

module.exports = {
  entry: {
    main: ['./src/index.js'],
    vendor: ['react', 'react-dom']
  },
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: '[name].js',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      }
    ]
  },
  resolve: {
    extensions: ['.js', '.jsx'],
  },
  plugins: [
    HtmlWebpackPluginConfig
  ],
  target: 'web'
};

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 5
  • Comments: 21 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I fixed this problem with: "babel-loader": "^8.0.0-beta"

I just ran into this problem, but I solved it by also installing @babel/register as a dependency.

So via https://github.com/babel/babel-upgrade

"devDependencies": {
  "@babel/core": "7.0.0-beta.39",
+ "babel-core": "7.0.0-bridge.0",
  "jest": "^22.0.0"
},
"scripts": {
  "test": "jest"
}

the solution is to add a new dep on "babel-core": "7.0.0-bridge.0", which interally uses @babel/core. This is what is done for jest. It might still not work if the module itself isn’t compatible with v7 though so would need some patches to jscodeshift.

If you believe to have issues between Babel versions, try to install this package

npm install -D babel-core@7.0.0-bridge.0

This usually fixes the problem between two versions among different dependencies that use different major versions of babel

I install @babel/register and solve the problem~~

Thing is that cli doesn’t have any dependency from Babel. It’s a dep of a dep, jscodeshift if we want to be more specific. Now I wonder if this is an issue related to that library

The most common reason for this issue is having the wrong babel-core installed. For version 7 you need to use @babel/core as opposed to babe/core.

npm uninstall babe-core
npm install @babel/core

It seems that yarn add babel-register@7.0.0-beta.3 or npm install babel-register@7.0.0-beta.3 may help. At least it works for me.