Ошибка при попытке обновить webpack3 до webpack4 - PullRequest
0 голосов
/ 05 октября 2018

Я получаю это сообщение об ошибке ниже, когда я обновил свой проект с веб-пакета 3.x до веб-пакета 4.0.0

    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

Конфигурации правил, как показано ниже в файле конфигурации

rules: [
  {
    test: /\.(jsx?)$/,
    use: 'babel-loader',
    exclude: /node_modules/,
  },
]

Package.json имеет следующие библиотеки

"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",
}

Буду признателен за любую помощь.

Ответы [ 2 ]

0 голосов
/ 05 октября 2018

Попробуйте очистить кэш node_modules и переустановить, если вы этого еще не сделали:

rm -rf node_modules/
rm -rf ~/.npm
npm cache verify
npm install
0 голосов
/ 05 октября 2018

В webpack4 конфигурация должна быть loader: "babel-loader", но не use: "babel-loader"

  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-cli": "^ 3.0.8",

"webpack-dev-server": "^ 3.1.4 ",

" babel-core ":" ^ 6.26.3 ",

" babel-loader ":" ^ 7.1.5 ",

" babel-plugin-transform-class-properties ":" ^ 6.24.1 ",

" babel-plugin-transform-object-rest-spread ":" ^ 6.26.0 ",

"babel-polyfill ":" ^ 6.26.0 "," babel-preset-env ":" ^ 1.7.0 ",

" babel-preset-реакции ":" ^ 6.24.1 "

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"
    }
};
...