忽略Grunt中的“未找到本地NPM模块”警告

时间:2014-03-04 15:10:14

标签: gruntjs npm

我正在使用 matchdep 从我的package.json文件中读取grunt的依赖项。

require('matchdep').filterAll('grunt-*').forEach(grunt.loadNpmTasks);

我的依赖项分为dependencies(适用于所有人)和devDependencies(适用于前端开发人员)。

我们的后端开发人员将运行以下内容来获取静态资产的构建,而不需要jasmine,phantomJS等(将由前端开发人员和CI服务器运行的东西)

$ npm install --production
$ grunt build

但是,使用--production版本时,grunt.loadNpmTasks()会针对任何缺失的包发出警告。

>> Local Npm module "grunt-contrib-watch" not found. Is it installed?

有没有办法压制此警告?

2 个答案:

答案 0 :(得分:1)

你必须质疑你的"后端开发者"必须实际构建你的包 - 否则,为什么他们需要grunt但不需要devDependencies。这有点向后(需要用户来构建你的软件包肯定是一种反模式)。

话虽如此,使用matchdep,你可以/应该使用:

  • require('matchdep').filter在您的"生产"目标
  • require('matchdep').filterAll在您的"开发"目标

当然,这需要你专攻你的grunt build(例如:有grunt builddevgrunt buildproduction - 或者可能使用环境变量) - 但是再次看到上面......

答案 1 :(得分:1)

您可以使用CLI标志将选项传递给grunt。为了保持一致性,我使用--production标志,就像我使用npm一样。

所以,从CLI:

$ grunt build --production

然后在Gruntfile中:

var dependencies;

// test for the production flag
if (grunt.option('production')) {
    // scan dependencies but ignore dev
    dependencies = require('matchdep').filter('grunt-*');
} else {
    // scan all dependencies
    dependencies = require('matchdep').filterAll('grunt-*');
}

// load only relevant dependencies
dependencies.forEach(grunt.loadNpmTasks);

在注册任何自定义任务之前,这是在模块顶部完成的。