首页 > 解决方案 > 运行 webpack 抛出“回调已被调用”错误

问题描述

我刚开始学习 webpack 来管理项目中的依赖项。我正在尝试使用它为我的打字稿和 javascript 文件构建捆绑包。对于打字稿文件,我使用ts-loader插件来处理它。对于 CSS,我正在使用mini-css-extractand 一个optimize-css-assets插件。当我尝试运行 webpack 时,我收到以下错误,我无法找出可能导致此错误的原因。

user@system spl % npm run build

> spl@1.0.0 build /Users/user/Downloads/spl
> webpack --config webpack.config.js

/Users/user/Downloads/spl/node_modules/neo-async/async.js:16
    throw new Error('Callback was already called.');
    ^

Error: Callback was already called.
    at throwError (/Users/user/Downloads/spl/node_modules/neo-async/async.js:16:11)
    at /Users/user/Downloads/spl/node_modules/neo-async/async.js:2818:7
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! spl@1.0.0 build: `webpack --config webpack.config.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the spl@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/user/.npm/_logs/2020-05-14T14_23_32_985Z-debug.log

以下是我用来构建 dist 文件的 webpack.config 文件。

const path = require('path');

const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const TerserPlugin = require('terser-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");

module.exports = {
    mode: 'production',
    entry: "./static/js/index.js",
    output: {
        filename: "bundle.[contentHash].js",  // File name with hash, based on content
        path: path.resolve(__dirname, 'dist')
    },
    optimization: {
        minimizer: [
            new OptimizeCssAssetsPlugin(),
            new TerserPlugin(),
            new HtmlWebpackPlugin({
                template: "./static/index.html",
                minify: {
                    removeAttributeQuotes: true,
                    collapseWhitespace: true,
                    removeComments: true
                }
            })
        ]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: "[name].[contentHash].css"
        }),
        new CleanWebpackPlugin(),
    ],
    module: {
        rules: [
            {
                test: /\.html$/,
                use: [ "html-loader" ]
            },
            {
                test: /\.[tj]s$/,
                use: "ts-loader",
                exclude: /(node_modules|tests)/
            },
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    "css-loader"
                ]
            }
        ],
    },
    resolve: {
        alias: {
            src: path.resolve(__dirname, 'src'),
            static: path.resolve(__dirname, 'static'),
        },
        extensions: [ '.ts', '.js' ]
    }
}

标签: node.jsnpmwebpack

解决方案


我有同样的问题,我意识到我正在从我的文件中导入 CSSindex.html文件:

<link rel="stylesheet" href="./css/main.css">

尽管 CSS 文件应该是从入口文件 ( index.js) 中导入的import

import '../css/main.css'; 

所以我删除了该行<link rel="stylesheet" href="./css/main.css">并解决了问题。您可以查看您的 HTML 文件并检查是否有任何从您的 HTML 文件导入的资产。我希望它有所帮助。


推荐阅读