Mikhail
Mikhail

Reputation: 49

Webpack can't resolve 'babel-loader'

everyone. I'm getting this error when trying to do npm run build. Here is the screen The whole process is a adding babel to webpack configuration. Here is the key files content. package.json

{
  "name": "learning-webpack",
  "version": "1.0.0",
  "description": "A package for learning Webpack",
  "main": "index.js",
  "scripts": {
    "build": "webpack --mode development",
    "watch": "webpack --w --mode development"
  },
  "author": "Mikhail Gilodo",
  "license": "ISC",
  "devDependencies": {
    "babel": "^6.23.0",
    "babel-core": "^6.26.3",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "loader": "^2.1.1",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.10"
  }
}

webpack.config.js

const path = require("path");                                       

module.exports = {
    entry: './src/index.js',                                        
    output: {
        filename: 'bundle.js',
        path: path.join(__dirname, '/dist')                        
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: ['es2015']
                    }
                }
            }
        ]
    }
}

.babelrc

{
    "presets": [
        "es2015"
    ]
}

Upvotes: 0

Views: 2755

Answers (2)

Mikhail
Mikhail

Reputation: 49

They added a new preset that has all the different presets

first run this code in your terminal or CMD

npm install --save-dev @babel/cli @babel/core @babel/preset-env @babel/register babel-loader

change your .babelrc file to this:

{
"presets": ["@babel/preset-env"]
}

finally your webpack.config.js file should look like this.

const path = require("path")

module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'bundle.js',
        path: path.join(__dirname, "/dist")
    }, 
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            }
        ]
    }
}

Upvotes: 1

Sterling Beason
Sterling Beason

Reputation: 630

It looks like you still need to install the "babel-loader" dependency. In the console:

npm install -D babel-loader

See the official docs for more information: https://webpack.js.org/loaders/babel-loader/

Upvotes: 1

Related Questions