多包./src/index.js ./dist/bundle.js中的webpack错误

时间:2018-02-27 10:26:06

标签: webpack

警告配置 '模式'选项尚未设置。设置'模式'选择开发'或者'生产'启用此环境的默认值。 多个./src/index.js ./dist/bundle.js中的错误

3 个答案:

答案 0 :(得分:33)

以下是在 webpack 4 中键入webpack --help来自webpack的帮助消息

Usage without config file: webpack <entry> [<entry>] --output [-o] <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"
  }
};

答案 1 :(得分:1)

我不确定这里的确切问题是什么,但我也有这个警告并通过在 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;

希望它有所帮助。

答案 2 :(得分:0)

有时无法在配置文件中设置模式。只需添加--mode=development就可以了。

"scripts": {
  "start": "webpack --config webpack.config.js --hot --mode=development",
}