且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

致命错误:无法找到Gruntfile

更新时间:2023-11-29 23:33:40

我想你要找的是实际在 Project Scaffolding 文档中找到的命令行工具 grunt-init p>

当前命令 grunt init 正在查找 Gruntfile.js 和其中的任务 init 。显然无法找到你的 Gruntfile.js ,所以引发了这个错误。



编辑:



您的新错误是因为您复制了 Gruntfile.js 文件而默认任务(如果你想打开它,在底部找到)将会调用这些模块。通常情况下,您将使用类似于 bower 的方式将它们安装到本地目录中。



我提到的默认任务上面看起来像这样:

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

这基本上是说:当我运行 grunt 在命令行运行任务 jshint qunit concat uglify 。这些任务将在前面的 Gruntfile 中指定。要了解该文件,请查看示例Gruntfile



我可以建议看看 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']


$ / code>

您应该仔细阅读每项任务的文档,并确保您了解你正在通过的选项。

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

 <%= jshint.gruntfile%> 

这是对 Gruntfile中特定文件/属性集的引用。 JS 。在这种情况下,这来自 watch 任务并指向 jshint.gruntfile 属性作为要观察的文件。

grunt --version

grunt-cli v0.1.8
grunt v0.4.1

$ npm -v

1.2.18

$ node -v

v0.10.6

When I run grunt init to create the Gruntfile.js, I get error:

$ 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.

I have searched for Grunfile.js, and I get:

/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

Can i just copy one of these grunt files, to /ex/co/www/dev/htdocs/unittest (where the Javascript / quint test are)? Or is there something else I am missing?

I am trying to run grunt on Centos 6.4. Also, why doesn't grunt init create the Gruntfile.js?

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

I copied it AS-IS and now I get better errors:

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.

mode progress....

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.

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

};

I think what you're looking for is the actual command line tool grunt-init found in the Project Scaffolding documentation.

Your current command grunt init is looking for a Gruntfile.js and the task init inside it. It is obviously unable to find your Gruntfile.js so is throwing that error.

Edit:

Your new error is because you have copied a Gruntfile.js file over and the default task (found near the bottom if you want to open it) will be calling those modules. Normally you will have used something like bower to install them into your local directory.

Your default task I referred to above will look something like this:

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

This is basically saying: when I run grunt at the command line, run the tasks jshint, qunit, concat and uglify in that order. These tasks will have been specified earlier on in the Gruntfile. To understand the file more check out a sample Gruntfile.

May I suggest taking a look at the Yeoman as it is a great tool to provide you with scaffolding for your apps (including a working Gruntfile.js).

Edit 2:

It is important you understand what is happening. The Gruntfile.js you have copied over looks to be calling "subgrunt" tasks. i.e. grunt tasks in sub projects. Is this what you are wanting?

Following through your Gruntfile.js - when you run grunt it is calling the following order: 'jshint', 'nodeunit', 'subgrunt'.

Ideally you want to write this yourself rather than copying it as there are some options being called by jshint I am unfamiliar with. If you read the jshint documentation it doesn't mention the first three options.

I think you should try something like this:

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']
  }
}

You should read through the documentation for each task and make sure you understand the options you are passing through.

Another hint. If you see a file reference like the following:

 <%= jshint.gruntfile %>

This is a reference to a particular file/property set in the Gruntfile.js. In this case, this came from the watch task and points to the jshint.gruntfile property as a file to watch.