webpack打包 The 'mode' option has not been set, webpack will fallback to

webpack 打包报错 The 'mode' option has not been set, webpack will fallback to 'production' for,Module not found: Error: Can't resolve,WARNING in configuration

运行环境:

  • windows
  • nodejs v10.15.3
  • webpack v4.38.0

我先全局安装了 webpack 和 webpack-cli:

npm install -g webpack
npm install -g webpack-cli

 

然后运行 webpack 去打包时报错:

webpack打包 The 'mode' option has not been set, webpack will fallback towebpack打包 The 'mode' option has not been set, webpack will fallback to

仔细看 webpack 官网手册发现,从4.0开始,运行 webpack 时一定要加参数 --mode development [开发环境] 或者 --mode production [生产环境] :

不使用配置文件的用法

webpack <entry> [<entry>] -o <output>
webpack打包 The 'mode' option has not been set, webpack will fallback to

<entry>

一个文件名或一组被命名的文件名,作为构建项目的入口起点。你可以传递多个入口(每个入口在启动时加载)。如果传递一个形式为 <name> = <request> 的键值对,则可以创建一个额外的入口起点。它将被映射到配置选项(configuration option)的 entry 属性。

<output>

要保存的 bundled 文件的路径和文件名。它将映射到配置选项 output.path 和 output.filename

示例

webpack --mode development app.js -o bundle.js

 

如果有配置文件(webpack.config.js),加入 mode 即可。

module.exports = {
    mode: 'development'
}
webpack打包 The 'mode' option has not been set, webpack will fallback to   再次打包:    

webpack打包 The 'mode' option has not been set, webpack will fallback towebpack打包 The 'mode' option has not been set, webpack will fallback to

成功!

附:

配置选项

参数 说明 输入类型 默认值
--mode 用到的模式,"development" 或 "production" 之中的一个 string  

 

上一篇:GHOST CMS - 配置 Config


下一篇:iOS单例模式