【问题标题】:grunt webpack Cannot find module 'webpack-dev-server'grunt webpack找不到模块'webpack-dev-server'
【发布时间】:2016-09-07 07:42:50
【问题描述】:

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

这是我的 grunt 文件:

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",

【问题讨论】:

  • webpack-dev-server grunt-webpack 的开发和对等依赖项。
  • 真的依赖吗?因为在文档中没有提到
  • 是的,请参阅 herehere。不过,它应该在您安装 grunt-webpack 时安装,或者至少应该给出未安装对等依赖项的警告。
  • 好的,谢谢。不知道为什么没有安装,我再试试看输出。

标签: gruntjs webpack webpack-dev-server


【解决方案1】:

您必须将其添加为依赖项。为什么你仍然不需要它;为了在开发中运行项目似乎有必要。这样,NPM 也允许您拥有开发依赖项。您列出的所有依赖项都是开发依赖项。

见下文:

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

【讨论】:

  • 这不是一个解决方案,它是一种黑客行为。如果我不使用它,那我为什么要把它包含在我的项目中。
  • 您正在使用它...您使用的软件包之一,使用它。所以你必须依赖它。有些东西需要模块并将其包含在您的 package.json 中不是黑客攻击。
猜你喜欢
  • 2017-07-04
  • 2021-04-23
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多