node.js 使用 npm 链接时如何修复“任务不在您的 gulpfile 中”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28972330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix "Task is not in your gulpfile" error when using npm link?
提问by zedix
We have a bunch of applications sharing common gulp logic, so we made a gulp plugin that contains a bunch of custom tasks.
我们有一堆共享通用 gulp 逻辑的应用程序,所以我们制作了一个包含一堆自定义任务的 gulp 插件。
However we'd like to avoid installing gulp+our plugin (along with half the internet) for each of the applications we develop.
但是,我们希望避免为我们开发的每个应用程序安装 gulp+我们的插件(以及一半的互联网)。
Ideally, I'd like to do:
理想情况下,我想做:
npm install -g gulp
npm install -g <our gulp plugin>
Then for each app, we'd simply have to do:
然后对于每个应用程序,我们只需要做:
npm link gulp
npm link <our gulp plugin>
Although this works, the problem is gulp no longer recognizes any of our custom gulp tasks. Any gulp command I run results in:
尽管这有效,但问题是 gulp 不再识别我们的任何自定义 gulp 任务。我运行的任何 gulp 命令都会导致:
[15:16:51] Using gulpfile /workspace/my-app/gulpfile.js
[15:16:51] Task 'dev' is not in your gulpfile
[15:16:51] Please check the documentation for proper gulpfile formatting
The 'dev' tasks is in my gulp plugin, why isn't it finding it? My gulpfile.js only has this:
“开发”任务在我的 gulp 插件中,为什么找不到它?我的 gulpfile.js 只有这个:
var gulp = require('gulp');
var mygulpplugin = require('mygulpplugin');
The exact same process works when gulp + the plugin is installed locally. Any ideas why?
当 gulp + 插件安装在本地时,完全相同的过程起作用。任何想法为什么?
采纳答案by zedix
Figured it out. Added the following line at the bottom of my module:
弄清楚了。在我的模块底部添加了以下行:
module.exports = gulp;
And my gulpfile in each module looks like this:
我在每个模块中的 gulpfile 看起来像这样:
var gulp = require('gulp');
var mygulpplugin = require('mygulpplugin');
gulp.tasks = mygulpplugin.tasks;
回答by jakub.g
Alternatively to the accepted answer, you can do it the way that was popular in grunt times, where you inject gulp to the plugin:
除了已接受的答案,您还可以采用在咕噜声中流行的方式,将 gulp 注入插件:
In plugin:wrap everything with:
在插件中:用以下内容包装所有内容:
module.exports = function(gulp) {
gulp.task('foo', function () {
...
})
...
}
and remove require('gulp')from the plugin's file.
并require('gulp')从插件文件中删除。
In gulpfile that depends on pluginyou then do:
在依赖插件的 gulpfile 中,您可以执行以下操作:
var gulp = require('gulp');
require('my-gulp-plugin')(gulp)
That way
那样
- you can have multiple plugins in the main gulpfile, because you don't override
gulp.tasks. - plugins don't have to declare
gulpapackage.jsondependency in each of the plugins (less work fornpm install)
- 您可以在主 gulpfile 中有多个插件,因为您不会覆盖
gulp.tasks. - 插件没有宣布
gulp一个package.json在每个插件的依赖(较少的工作npm install)

