14

咕噜--版本

grunt-cli v0.1.8
grunt v0.4.1

$ npm -v

1.2.18

$节点-v

v0.10.6

当我运行grunt init创建时Gruntfile.js,我得到错误:

$ grunt init 
A valid Gruntfile could not be found. Please see the getting started guide for
more information on how to configure grunt: http://gruntjs.com/getting-started
Fatal error: Unable to find Gruntfile.

我搜索了 Grunfile.js,我得到:

/home/ka/.npm/grunt-cli/0.1.8/package/Gruntfile.js
/home/ka/tmp/npm-1464/1368671910572-0.38816986070014536/package/Gruntfile.js
/usr/local/lib/node_modules/grunt/Gruntfile.js
/usr/local/lib/node_modules/grunt/node_modules/findup-sync/Gruntfile.js
/usr/local/lib/node_modules/grunt-cli/Gruntfile.js
/usr/local/lib/node_modules/grunt-cli/node_modules/findup-sync/Gruntfile.js
/ex/co/www/dev/htdocs/unittest/node_modules/grunt/node_modules/findup-sync/Gruntfile.js
/ex/co/www/dev/htdocs/unittest/node_modules/grunt/Gruntfile.js
/root/.npm/findup-sync/0.1.2/package/Gruntfile.js
/root/.npm/grunt/0.4.1/package/Gruntfile.js
/root/.npm/grunt-cli/0.1.8/package/Gruntfile.js

我可以将这些 grunt 文件之一复制到 /ex/co/www/dev/htdocs/unittest (Javascript / quint 测试所在的位置)吗?还是我还缺少其他东西?

我正在尝试在 Centos 6.4 上运行 grunt。另外,为什么不grunt init创建Gruntfile.js

cp /root/.npm/grunt/0.4.1/package/Gruntfile.js /ex/co/www/dev/htdocs/unittest/

我按原样复制了它,现在我得到了更好的错误:

grunt 
>> Local Npm module "grunt-contrib-jshint" not found. Is it installed?
>> Local Npm module "grunt-contrib-nodeunit" not found. Is it installed?
>> Local Npm module "grunt-contrib-watch" not found. Is it installed?
Warning: Task "jshint" not found. Use --force to continue.

Aborted due to warnings.

模式进度....

grunt 
Running "jshint:gruntfile" (jshint) task
Warning: Cannot call method 'forEach' of undefined Use --force to continue.

Aborted due to warnings.
kahmed@vm-devqa01 /ex/co/www/dev/htdocs/unittest $ grunt --force
Running "jshint:gruntfile" (jshint) task
Warning: Cannot call method 'forEach' of undefined Used --force, continuing.
Warning: Cannot call method 'forEach' of undefined Used --force, continuing.

Running "jshint:libs_n_tests" (jshint) task
Warning: Cannot call method 'forEach' of undefined Used --force, continuing.
Warning: Cannot call method 'forEach' of undefined Used --force, continuing.

Running "jshint:subgrunt" (jshint) task
Warning: Cannot call method 'forEach' of undefined Used --force, continuing.
Warning: Cannot call method 'forEach' of undefined Used --force, continuing.

Running "nodeunit:all" (nodeunit) task
Warning: Cannot call method 'map' of undefined Used --force, continuing.
Warning: Cannot call method 'map' of undefined Used --force, continuing.

Running "subgrunt:all" (subgrunt) task
Warning: Cannot read property 'length' of undefined Used --force, continuing.
Warning: Cannot read property 'length' of undefined Used --force, continuing.

Done, but with warnings.

我的 Gruntfile.js:

/*
 * grunt
 * http://gruntjs.com/
 *
 * Copyright (c) 2013 "Cowboy" Ben Alman
 * Licensed under the MIT license.
 * https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
 */

'use strict';

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    nodeunit: {
      all: ['test/{grunt,tasks,util}/**/*.js']
    },
    jshint: {
      gruntfile: ['Gruntfile.js'],
      libs_n_tests: ['lib/**/*.js', '<%= nodeunit.all %>'],
      subgrunt: ['<%= subgrunt.all %>'],
      options: {
        curly: true,
        eqeqeq: true,
        immed: true,
        latedef: true,
        newcap: true,
        noarg: true,
        sub: true,
        undef: true,
        unused: true,
        boss: true,
        eqnull: true,
        node: true,
        es5: true
      }
    },
    watch: {
      gruntfile: {
        files: ['<%= jshint.gruntfile %>'],
        tasks: ['jshint:gruntfile']
      },
      libs_n_tests: {
        files: ['<%= jshint.libs_n_tests %>'],
        tasks: ['jshint:libs_n_tests', 'nodeunit']
      },
      subgrunt: {
        files: ['<%= subgrunt.all %>'],
        tasks: ['jshint:subgrunt', 'subgrunt']
      }
    },
    subgrunt: {
      all: ['test/gruntfile/*.js']
    },
  });

  // These plugins provide necessary tasks.
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-nodeunit');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-qunit');

  grunt.registerTask('test', 'qunit:src');

  // "npm test" runs these tasks
  grunt.registerTask('test', ['jshint', 'nodeunit', 'subgrunt']);

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

  // Run sub-grunt files, because right now, testing tasks is a pain.
  grunt.registerMultiTask('subgrunt', 'Run a sub-gruntfile.', function() {
    var path = require('path');
    grunt.util.async.forEachSeries(this.filesSrc, function(gruntfile, next) {
      grunt.util.spawn({
        grunt: true,
        args: ['--gruntfile', path.resolve(gruntfile)],
      }, function(error, result) {
        if (error) {
          grunt.log.error(result.stdout).writeln();
          next(new Error('Error running sub-gruntfile "' + gruntfile + '".'));
        } else {
          grunt.verbose.ok(result.stdout);
          next();
        }
      });
    }, this.async());
  });

};
4

5 回答 5

14

我遇到了同样的错误,结果我只需要先运行 npm install 。尽管我之前已经安装了 npm,但安装时有些东西已经损坏。快速重新安装工作;我只需要这两个命令:

npm install
grunt jshint
于 2014-06-30T15:14:28.503 回答
13

我认为您正在寻找的是在Project Scaffolding文档grunt-init中找到的实际命令行工具。

您当前的命令grunt init正在寻找 aGruntfile.js和其中的任务init。显然无法找到您的Gruntfile.js所以抛出该错误。

编辑:

您的新错误是因为您已经复制了一个Gruntfile.js文件并且default任务(如果您想打开它可以在底部附近找到)将调用这些模块。通常,您会使用类似bower将它们安装到本地目录中的方法。

我上面提到的您的默认任务将如下所示:

grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']);

这基本上是说:当我grunt在命令行运行时,按顺序运行任务jshint、、和。这些任务将在前面的. 要了解该文件,请查看示例 GruntfilequnitconcatuglifyGruntfile

我建议您看看Yeoman,因为它是一个很好的工具,可以为您的应用程序(包括工作Gruntfile.js)提供脚手架。

编辑2:

了解正在发生的事情很重要。您复制的Gruntfile.js看起来正在调用“subgrunt”任务。即grunt子项目中的任务。这是你想要的吗?

遵循您的Gruntfile.js- 当您运行grunt它时,它会调用以下命令:'jshint', 'nodeunit', 'subgrunt'.

理想情况下,您想自己编写而不是复制它,因为jshint我不熟悉某些选项。如果您阅读jshint 文档,它没有提到前三个选项。

我认为你应该尝试这样的事情:

jshint: {
  options: {
    curly: true,
    eqeqeq: true,
    immed: true,
    latedef: true,
    newcap: true,
    noarg: true,
    sub: true,
    undef: true,
    unused: true,
    boss: true,
    eqnull: true,
    node: true,
    es5: true
  }
  files: {
    src: ['path/to/your/*.js', 'another/path/to/your/*.js']
  }
}

您应该通读每项任务的文档,并确保您了解所传递的选项。

另一个提示。如果您看到如下文件引用:

 <%= jshint.gruntfile %>

这是对Gruntfile.js. 在这种情况下,这来自watch任务并指向jshint.gruntfile属性作为要监视的文件。

于 2013-05-17T14:32:23.447 回答
0

我有一个类似的问题,我使用了下面的命令,它对我有用

grunt -f 
grunt server

希望它也对你有用。

于 2014-07-31T11:27:41.023 回答
0

Grunt 文件应该在根目录中,而不是在某个文件夹中,也应该拼写为 "Gruntfile.js"

例子

如果是 asp.net Core angular 项目,它的位置应该是。

在此处输入图像描述

如果您将此文件放在 ClientApp 或其他地方,直到您定义一些方法来执行此操作,它才会起作用。

于 2019-08-31T21:37:30.723 回答
-1

检查您是否在您正在处理的目录中并在那里运行 npm install 它可能会解决您的问题

于 2016-04-01T19:45:59.970 回答