webpack配置踩坑笔记

我package.json.js中的scripts配置:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "dev": "webpack-dev-server"
  }

1.运行npm run build时TypeError: Cannot destructure property ‘createHash’ of ‘undefined’ or ‘null’. at Object.


出现这一问题是因为webpack的版本问题,我出现这个问题时webpack的版本是webpack的版本是4.28.1,使用cnpm i webpack@^4.3.0后就可以正常运行了
github上有相关的讨论:https://github.com/webpack-contrib/mini-css-extract-plugin/issues/69

2.运行run npm build时D:\node.js\node_global\webpack-cli:2basedir= ( d i r n a m e " (dirname " (dirname"(echo “$0” | sed -e ‘s,\,/,g’)") SyntaxError: missing ) after argument list

在这里插入图片描述
这个问题看似是在文件中少了一个括号,但是实际上是因为本地项目没有安装对应的包,这里在本地安装即可cnpm i -D webpack-cli

3. TypeError: CleanWebpackPlugin is not a constructor at Object.

出现这个问题一般是因为导入CleanWebpackPlugin 错误
在这里插入图片描述
我本来导入的代码

const CleanWebpackPlugin = require('clean-webpack-plugin');

但是查看github中的文档中的用法

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

const webpackConfig = {
    plugins: [
        /**
         * All files inside webpack's output.path directory will be removed once, but the
         * directory itself will not be. If using webpack 4+'s default configuration,
         * everything under <PROJECT_DIR>/dist/ will be removed.
         * Use cleanOnceBeforeBuildPatterns to override this behavior.
         *
         * During rebuilds, all webpack assets that are not used anymore
         * will be removed automatically.
         *
         * See `Options and Defaults` for information
         */
        new CleanWebpackPlugin(),
    ],
};

module.exports = webpackConfig;

可以看到实际的用法应该是

const { CleanWebpackPlugin } = require('clean-webpack-plugin')
// 或者
const CleanWebpackPlugin = require('clean-webpack-plugin').CleanWebpackPlugin

去网上找了找源码,可以明显看出导出的是CleanWebpackPlugin,所以必须使用CleanWebpackPlugin这个名字

declare class CleanWebpackPlugin {
    private readonly dry;
    private readonly verbose;
    private readonly cleanStaleWebpackAssets;
    private readonly protectWebpackAssets;
    private readonly cleanAfterEveryBuildPatterns;
    private readonly cleanOnceBeforeBuildPatterns;
    private readonly dangerouslyAllowCleanPatternsOutsideProject;
    private currentAssets;
    private initialClean;
    private outputPath;
    constructor(options?: Options);
    apply(compiler: Compiler): void;
    /**
     * Initially remove files from output directory prior to build.
     *
     * Only happens once.
     *
     * Warning: It is recommended to initially clean your build directory outside of webpack to minimize unexpected behavior.
     */
    handleInitial(compilation: Compilation): void;
    handleDone(stats: Stats): void;
    removeFiles(patterns: string[]): void;
}
export { CleanWebpackPlugin };

4. ERROR in ./src/main.less Module build failed: ModuleBuildError: Module build failed: Error: No PostCSS Config found in:

在这里插入图片描述
出现这个错误的原因是没有postcss的配置文件
在根目录新建postcss.config.js,并在里面写入配置即可
配置举例

const autoprefixer = require("autoprefixer")
module.exports = {
    plugins: [autoprefixer]
}

5. 执行npm run dev时Error: Cannot find module ‘webpack-cli/bin/config-yargs’

在这里插入图片描述
造成这个错误的原因是webpack和webpack-dev-server版本不匹配,执行下面两句

cnpm i -D webpack
cnpm i -D webpack-dev-server

问题解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值