How to fix “Task is not in your gulpfile” error when using npm link?

痞子三分冷 提交于 2019-12-04 17:38:19

问题


We have a bunch of applications sharing common gulp logic, so we made a gulp plugin that contains a bunch of custom tasks.

However we'd like to avoid installing gulp+our plugin (along with half the internet) for each of the applications we develop.

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:

[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:

var gulp = require('gulp');
var mygulpplugin = require('mygulpplugin');

The exact same process works when gulp + the plugin is installed locally. Any ideas why?


回答1:


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:

var gulp = require('gulp'); 
var mygulpplugin = require('mygulpplugin');
gulp.tasks = mygulpplugin.tasks;



回答2:


Alternatively to the accepted answer, you can do it the way that was popular in grunt times, where you inject gulp to the plugin:

In plugin: wrap everything with:

module.exports = function(gulp) {
    gulp.task('foo', function () {
        ...
    })
    ...
}

and remove require('gulp') from the plugin's file.

In gulpfile that depends on plugin you then do:

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 gulp a package.json dependency in each of the plugins (less work for npm install)


来源:https://stackoverflow.com/questions/28972330/how-to-fix-task-is-not-in-your-gulpfile-error-when-using-npm-link

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!