Webpack无法解析“ babel-loader”

时间:2019-11-20 15:29:35

标签: webpack babeljs babel-loader

每个人。 尝试执行npm run build时出现此错误。 这是screen 整个过程为webpack配置增加了麻烦。 这是关键文件的内容。 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"
    ]
}

2 个答案:

答案 0 :(得分:1)

您似乎仍然需要安装“ babel-loader”依赖项。在控制台中:

npm install -D babel-loader

有关更多信息,请参见官方文档:https://webpack.js.org/loaders/babel-loader/

答案 1 :(得分:0)

他们添加了具有所有不同预设的新预设

首先在您的终端或CMD中运行此代码

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

将.babelrc文件更改为此:

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

最后,您的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'
                }
            }
        ]
    }
}