node.js grunt 错误:找不到模块“load-grunt-tasks”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28385685/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:23:55  来源:igfitidea点击:

grunt error: cannot find module 'load-grunt-tasks'

node.jsmean-stack

提问by silent_programmer

When I am using grunt command it is showing me following error:

当我使用 grunt 命令时,它显示以下错误:

$ grunt
Loading "Gruntfile.js" tasks...ERROR
>> Error: Cannot find module 'load-grunt-tasks'
Warning: Task "default" not found. Use --force to continue.

Aborted due to warnings.


Execution Time (2015-02-07 18:05:42 UTC)
loading tasks  339ms  ███████████████████████████████████████████████ 99%
Total 344ms

I already tried - npm install, npm update commands. It would be great if someone can help me with this. Thanks!

我已经尝试过 - npm install、npm update 命令。如果有人能帮我解决这个问题,那就太好了。谢谢!

Addding Content of Gruntfile.js

添加 Gruntfile.js 的内容

'use strict';

var paths = {
  js: ['*.js', 'test/**/*.js', '!test/coverage/**', '!bower_components/**', 'packages/**/*.js', '!packages/**/node_modules/**', '!packages/contrib/**/*.js', '!packages/contrib/**/node_modules/**'],
  html: ['packages/**/public/**/views/**', 'packages/**/server/views/**'],
  css: ['!bower_components/**', 'packages/**/public/**/css/*.css', '!packages/contrib/**/public/**/css/*.css']
};

module.exports = function(grunt) {

  if (process.env.NODE_ENV !== 'production') {
    require('time-grunt')(grunt);
  }

  // Project Configuration
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    assets: grunt.file.readJSON('config/assets.json'),
    clean: ['bower_components/build'],
    watch: {
      js: {
        files: paths.js,
        tasks: ['jshint'],
        options: {
          livereload: true
        }
      },
      html: {
        files: paths.html,
        options: {
          livereload: true,
          interval: 500
        }
      },
      css: {
        files: paths.css,
        tasks: ['csslint'],
        options: {
          livereload: true
        }
      }
    },
    jshint: {
      all: {
        src: paths.js,
        options: {
          jshintrc: true
        }
      }
    },
    uglify: {
      core: {
        options: {
          mangle: false
        },
        files: '<%= assets.core.js %>'
      }
    },
    csslint: {
      options: {
        csslintrc: '.csslintrc'
      },
      src: paths.css
    },
    cssmin: {
      core: {
        files: '<%= assets.core.css %>'
      }
    },
    nodemon: {
      dev: {
        script: 'server.js',
        options: {
          args: [],
          ignore: ['node_modules/**'],
          ext: 'js,html',
          nodeArgs: ['--debug'],
          delayTime: 1,
          cwd: __dirname
        }
      }
    },
    concurrent: {
      tasks: ['nodemon', 'watch'],
      options: {
        logConcurrentOutput: true
      }
    },
    mochaTest: {
      options: {
        reporter: 'spec',
        require: [
          'server.js',
          function() {
            require('meanio/lib/core_modules/module/util').preload(__dirname + '/packages/**/server', 'model');
          }
        ]
      },
      src: ['packages/**/server/tests/**/*.js']
    },
    env: {
      test: {
        NODE_ENV: 'test'
      }
    },
    karma: {
      unit: {
        configFile: 'karma.conf.js'
      }
    }
  });

  //Load NPM tasks
  require('load-grunt-tasks')(grunt);

  /**
   * Default Task
   */
  grunt.hook.push('clean', -9999);
  grunt.hook.push('concurrent', 9999);
  if (process.env.NODE_ENV === 'production') {
    grunt.hook.push('cssmin', 100);
    grunt.hook.push('uglify', 200);
  } else {
    grunt.hook.push('jshint', -200);
    grunt.hook.push('csslint', 100);
  }

  //Default task.
  grunt.registerTask('default', ['hook']);

  //Test task.
  grunt.registerTask('test', ['env:test', 'mochaTest', 'karma:unit']);

  // For Heroku users only.
  // Docs: https://github.com/linnovate/mean/wiki/Deploying-on-Heroku
  grunt.registerTask('heroku:production', ['cssmin', 'uglify']);
};

回答by Roland Hudák

Try running:

尝试运行:

$ npm install

After that, if you run it and the error still persists or if there's another one, then you probably have not installed ruby, compass or both :)

在那之后,如果您运行它并且错误仍然存​​在,或者还有另一个错误,那么您可能没有安装 ruby​​、compass 或两者:)

回答by David J. Davis

I had the same issue, the problem for me was in my package.json where I didn't actually install the NPM package needed and it was not automatically installed as previously thought. Try doing

我有同样的问题,对我来说问题出在我的 package.json 中,我实际上没有安装所需的 NPM 包,而且它没有像以前想象的那样自动安装。尝试做

npm install --save-dev load-grunt-tasks

If that doesn't work can you provide the package.json file as well so we can get a little more information.

如果这不起作用,您能否也提供 package.json 文件,以便我们获得更多信息。

回答by Bennett Dill

I was having the same issue you were having, it seems as though the gruntfile is missing a required initialization step.

我遇到了与您相同的问题,似乎 gruntfile 缺少必需的初始化步骤。

By changing this:

通过改变这个:

require('load-grunt-tasks')(grunt);

/**
 * Default Task
 */
grunt.hook.push('clean', -9999);

to this:

对此:

require('load-grunt-tasks')(grunt);

grunt.loadNpmTasks('grunt-hook');

/**
 * Default Task
 */
grunt.hook.push('clean', -9999);

Adding the grunt.loadNpmTasks call, I'm able to get past that issue. The problem is, now I'm getting

添加 grunt.loadNpmTasks 调用,我能够解决这个问题。问题是,现在我得到

Task "clean" not found. Use --force to continue.

未找到任务“干净”。使用 --force 继续。

Looking at the rest of the grunt file, i don't see a register task for clean. If I go to the mean.io docs, it looks like the build is failing. Perhaps this is part of why? I think I asked mean-cli for gulp version, that's probably why. I'll delete and take it from the top :)

查看 grunt 文件的其余部分,我没有看到清理的注册任务。如果我去mean.io docs,看起来构建失败了。也许这就是为什么?我想我向 mean-cli 询问了 gulp 版本,这可能就是原因。我会删除并从顶部取出它:)

回答by Paolo Carrasco

I think the problem is related to where the npm dependencies are declared and the way Heroku handles them. In few words, check if the npm packages are as dev dependencies and move them to the dependencies block, as suggested here: https://stackoverflow.com/a/20687098/532912.

我认为问题与 npm 依赖项的声明位置以及 Heroku 处理它们的方式有关。简而言之,检查 npm 包是否作为开发依赖项并将它们移动到依赖项块,如这里建议的:https: //stackoverflow.com/a/20687098/532912