javascript - Grunt 任务依赖

标签 javascript npm gruntjs

我有一个通过 npm taskA(不是实际名称)安装的 grunt 任务

taskA 有一个依赖项:grunt-contrib-stylus,它在 taskA 的 package.json 中指定并安装。出于某种原因,当从主 Gruntfile.js 运行 grunt default 时出现错误。

警告:找不到任务“stylus”。使用 --force 继续。

修复方法是在主项目中需要 grunt-contrib-stylus。我想避免这种情况。我的任务没有在其 node_modules/中使用 grunt-contrib-stylus 的原因是什么?

任务A

module.exports = function(grunt) {
'use strict';

grunt.loadNpmTasks('grunt-contrib-stylus');
...

主 Gruntfile.js

...
grunt.loadNpmTasks('taskA');
...

最佳答案

grunt.loadNpmTasks 加载 [cwd]/node_modules/[modulename]/tasks/。您可以通过更改 cwd 将任务加载为依赖项:

任务A

module.exports = function(grunt) {
  var parentcwd = process.cwd();
  process.chdir(__dirname);

  grunt.loadNpmTasks('grunt-contrib-stylus');

  process.chdir(parentcwd);
};

只需确保在最后将 cwd 设置回父级即可。

关于javascript - Grunt 任务依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17588466/

相关文章:

javascript - 调用针对 DOM 的自定义方法时出现的问题

node.js - 列出没有 devDependencies 的 Node 包依赖项

css - 如何禁用 sass --watch 启动的文件夹

node.js - 正在加载 "connect_proxy.js"任务...错误 >> TypeError : Cannot read property 'prototype' of undefined

Javascript p5.js 类未正确绘制

javascript - Angular 5 循环依赖 : Component in Component

javascript - Grunt Wiredep 不更新脚本标签

javascript - Grunt - fatal error 未定义不是函数

JavaScript嵌套对象数组,如何循环遍历数组的 'KEY'?

javascript - webpack 2 + Babel 没有转译为 es2015