grunt webpack找不到模块'webpack-dev-server'

时间:2016-09-07 07:42:50

标签: gruntjs webpack webpack-dev-server

我有一个grunt设置来使用webpack和grunt-webpack捆绑我的反应文件。我没有使用webpack-dev-server,但我的咕噜声仍在输出错误Cannot find module 'webpack-dev-server'

这是我的咕噜文件:

var webpack = require("webpack");
var webpackConfig = require("./webpack.config.js");

module.exports = function(grunt) {
  require('load-grunt-tasks')(grunt);
  grunt.loadNpmTasks("grunt-webpack");

  grunt.initConfig({
    eslint: {
      options: {
        configFile: 'eslint.json'
      },
      target: ['./react/components/**/*.js','./react/services/**/*.js', './node/source/**/*.js']
    },
    webpack: {
      options: webpackConfig,
      build:{}
    },
    watch: {
      app: {
        files: ['./react/main.js', './react/components/**/*.js','./react/services/**/*.js'],
        tasks: ["webpack"],
        options: {
          spawn: false,
        }
      }
    }
  });

  grunt.registerTask('lint', ['eslint']);
  grunt.registerTask('build', ['webpack']);
  grunt.registerTask('default', ['watch']);

};

webpack.config.js档案

const path = require('path');
const buildDirectory = './node/static/js/';

module.exports = {
    entry: './react/main.js',
    resolve: {
        extensions: ['', '.js', '.jsx'],
    },
    output: {
        path: path.resolve(buildDirectory),
        filename: 'bundle.js',
    },
    externals: {
        'cheerio': 'window',
        'react/lib/ExecutionEnvironment': true,
        'react/lib/ReactContext': true,
    },
    stats: {
        assets: false,
        colors: false,
        modules: false,
        version: false,
        hash: false,
        timings: false,
        chunks: false,
        chunkModules: false
    },
    module: {
        loaders: [{
            test: /\.jsx?$/,
            exclude: /(node_modules|bower_components)/,
            loader: 'babel',
            query: {
                presets: ['react', 'es2015'],
            },
        }],
    },
    plugins: [],
};

错误输出

grunt build
Loading "webpack-dev-server.js" tasks...ERROR
>> Error: Cannot find module 'webpack-dev-server'
Loading "webpack-dev-server.js" tasks...ERROR
>> Error: Cannot find module 'webpack-dev-server'

Running "webpack:build" (webpack) task


Done.

如果我添加webpack-dev-server包,则会解决此错误,但我不想在项目中添加额外的依赖项。

的软件包:

"webpack": "^1.13.1",
"grunt-webpack": "^1.0.14",
"grunt": "^1.0.1",
"grunt-contrib-watch": "^1.0.0",
"grunt-eslint": "^19.0.0",

1 个答案:

答案 0 :(得分:2)

您必须将其添加为依赖项。你为什么不需要它呢?似乎有必要为了在开发中运行项目。这就是NPM允许您拥有开发依赖关系的方式。您列出的所有内容都是dev依赖项。

见下文:

  "devDependencies": {
    // ... more dev dependencies
    "webpack-dev-server": "^1.14.1"
  },
  "dependencies": {
    // ... more 'normal' dependencies
    "bootstrap": "^3.3.6"
  }