致命错误:无法找到Gruntfile

13

grunt --version

grunt-cli v0.1.8
grunt v0.4.1

$ npm -v

1.2.18

$ node -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());
  });

};
5个回答

13

我认为你正在寻找的是实际的命令行工具grunt-init,可以在“项目脚手架”文档中找到。

你当前的命令grunt init正在查找一个包含任务initGruntfile.js。显然它无法找到你的Gruntfile.js,所以会抛出错误。

编辑:

你的新错误是因为你复制了一个Gruntfile.js文件,而default任务(如果你想打开它则可以在底部找到)将会调用那些模块。通常你会使用像bower这样的工具将它们安装到本地目录中。

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

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

这基本上是在说:当我在命令行中运行grunt时,按照顺序运行任务jshintqunitconcatuglify。这些任务会在Gruntfile中提前指定。要了解该文件的更多信息,请查看示例Gruntfile

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

编辑2:

重要的是您要理解正在发生的事情。Gruntfile.js看起来在调用“子grunt”任务,即子项目中的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属性指向一个要监视的文件。


是的,Yeoman看起来很有前途。我肯定会尝试一下。我只需要完成这个grunt的事情,才能继续前进。我想这是我的强迫症和过度专注的本性 :) 所以我要更新原始文本,因为我取得了一些进展。 - kamal
很好 - 是的,请更新您的问题,我会再次查看它。我的回答有意义/对您有帮助吗? - Ewan

13

我遇到了同样的错误,后来发现我只需要先运行npm install命令。尽管我之前已经安装了npm,但是安装出现了问题。重新安装就解决了问题;这两个命令就足够了:

npm install
grunt jshint

1
这就是我要说的了!谢谢! - Sander Versluys
2
谢谢您,npm install 让我摆脱了痛苦。 - Tobias Sarnow

0

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

示例

对于ASP.NET Core Angular项目,它应该位于以下位置。

enter image description here

如果您不定义某些方法来执行此操作,将无法在ClientApp或其他任何地方放置此文件。


0

我曾经遇到过类似的问题,我使用了以下命令,然后问题得到了解决。

grunt -f 
grunt server

希望它也能对你有用。

-1

请检查您当前所在的目录是否正确,并在该目录下运行npm install,这样可能会解决您的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接