警告:配置中缺少'mode'选项。请将'mode'选项设置为'development'或'production',以启用此环境的默认设置。错误:在多个入口文件中发现重复的./src/index.js和./dist/bundle.js。
以下是在webpack 4中键入webpack --help
后来自webpack的帮助信息
Usage without config file: webpack <entry> [<entry>] --output [-o] <output>
注意:必须明确指定--output参数
解决方案:
webpack src/index.js --output dist/bundle.js --mode development
如果您正在使用 webpack.config.js
:
const path = require('path');
module.exports = {
mode: 'development', // set mode option, 'development' or 'production'
entry: './src/index.js',
output: {
path: path.resolve(__dirname,'dist'),
filename: "bundle.js"
}
};
我不确定这里确切的问题是什么,但我也遇到了这些警告,并通过在Webpack配置文件中设置mode
属性来解决它们。
package.json
{
"name": "my-awesome-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "NODE_ENV=production webpack",
},
...
"devDependencies": {
...
},
"dependencies": {
...
}
}
webpack.config.js
const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const distDir = path.join(__dirname, 'dist');
const config = {
mode: 'development',
entry: ['./src/js/app.js'],
output: {
path: distDir,
filename: 'js/[name].js',
},
module: {
rules: [
...
]
},
plugins: [
...
],
devtool: "eval-source-map", // Default development sourcemap
};
// Check if build is running in production mode, then change the sourcemap type
if (process.env.NODE_ENV === 'production') {
config.devtool = ''; // No sourcemap for production
config.mode = 'production';
// Add more configuration for production here like
// Uglify plugin
// Offline plugin
// Etc,
}
module.exports = config;
在配置文件中设置模式有时不起作用。只需添加--mode=development
即可解决问题。
"scripts": {
"start": "webpack --config webpack.config.js --hot --mode=development",
}
webpack v4
有关。请查阅他们的文档以了解如何迁移到webpack v4
。 - Ru Chern Chong