首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我试图从webpack3升级到webpack4时出错

当我试图从webpack3升级到webpack4时出错
EN

Stack Overflow用户
提问于 2018-10-05 06:53:38
回答 2查看 798关注 0票数 2

当我将我的项目从webpack 3.x升级到webpack 4.0.0时,我得到了下面的错误

代码语言:javascript
复制
    ERROR in multi script-loader!jquery/dist/jquery.min.js script-loader!foundation-sites/dist/js/foundation.min.js eventsource-polyfill webpack-hot-middleware/client?reload=true ./src/index.jsx
Module not found: Error: Can't resolve 'babel-loader' in 'C:\projects\rebasing\uisrc'
 @ multi script-loader!jquery/dist/jquery.min.js script-loader!foundation-sites/dist/js/foundation.min.js eventsource-polyfill webpack-hot-middleware/client?reload=true ./src/index.jsx

这些规则配置如下面的配置文件所示

代码语言:javascript
复制
rules: [
  {
    test: /\.(jsx?)$/,
    use: 'babel-loader',
    exclude: /node_modules/,
  },
]

Package.json有以下库

代码语言:javascript
复制
"devDependencies": {
"babel-cli": "6.26.0",
"babel-core": "6.8.0",
"babel-eslint": "7.0.0",
"babel-loader": "7.1.5",
"babel-preset-es2015": "6.6.0",
"babel-preset-react": "6.5.0",
"babel-preset-react-hmre": "1.1.1",
"babel-preset-stage-0": "6.5.0",
"babel-preset-stage-2": "6.17.0",
"babel-register": "6.26.0",
}

任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-05 07:16:44

在webpack4中,配置应该是loader: "babel-loader",而不是use: "babel-loader"

代码语言:javascript
复制
  rules: [
        {
            //tell webpack to use jsx-loader for all *.jsx files
            test: /\.(jsx?)$/,
            exclude: /node_modules/,
            loader: "babel-loader"
        },
   ]

这是我的webpack4工作演示

我正在使用的版本

webpack:"^4.15.0", “webpack”:"^3.0.8", webpack开发服务器:“^3.1.4”, "babel-core":"^6.26.3", "babel-loader":"^7.1.5", "babel-plugin-transform-class-properties":"^6.24.1", “babel-插件-转换-对象-rest-扩展”:"^6.26.0", "babel-polyfill":"^6.26.0",“babel-预设-env”:"^1.7.0", “预置反应”:"^6.24.1“

代码语言:javascript
复制
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CompressionPlugin = require("compression-webpack-plugin");
const webpack = require('webpack');

module.exports = {
    target: "web",
    entry: [
        "whatwg-fetch",
        'webpack-dev-server/client?http://localhost:8090',
        'webpack/hot/only-dev-server',
        'babel-polyfill',
        "./src/index.js"
        ],
    output: {
        path: path.resolve(__dirname, "build"),
        filename: "bundle.js",
        publicPath: "/"
        //make sure port 8090 is used when launching webpack-dev-server
    },
    plugins: [new HtmlWebpackPlugin({
        template: "index.html"
    }),
    new CompressionPlugin({
        asset: "[path].gz[query]",
        algorithm: "gzip",
        test: /\.js$|\.jsx$|\.css$|\.html$/,
        threshold: 10240,
        minRatio: 0.8
    }),
    new webpack.HotModuleReplacementPlugin(),
    // enable HMR globally

    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.ProvidePlugin({   
        jQuery: 'jquery',
        $: 'jquery',
        jquery: 'jquery'
    })
    ],
    module: {
        rules: [
            {
                //tell webpack to use jsx-loader for all *.jsx files
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                loader: "babel-loader"
            },
            {
                test: /\.css$/,
                loader: "style-loader!css-loader"
            },
            {
                test: /\.(png|jpg|jpeg|gif|woff|woff2|svg)$/,
                loader: 'url-loader?limit=100000'
            },
            {
                test: /\.(eot|ttf)$/,
                loader: "file-loader"  
            },
            {
                test: /\.html$/,
                exclude: /node_modules/,
                loader: 'html-loader'
            },
            {
                test: /\.scss$/,
                loaders: ["style-loader", "css-loader", "sass-loader"]
            }
        ]
    },
    resolve: {
        modules: [
            path.resolve("./src"),
            path.resolve("./node_modules")
        ],
        extensions: [".js", ".jsx"]
    },
    devServer: {
        watchOptions: {
        // Needed for Windows Subsystem for Linux dev environment:
            poll: true
        },
        contentBase: "/build"
    },
    devtool: "cheap-module-eval-source-map",
    node: {
        child_process : "empty",
        fs: "empty"
    }
};
票数 1
EN

Stack Overflow用户

发布于 2018-10-05 18:02:50

如果还没有,请尝试清除node_modules缓存并重新安装:

代码语言:javascript
复制
rm -rf node_modules/
rm -rf ~/.npm
npm cache verify
npm install
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52659893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档