致命错误:无法找到Gruntfile

kam*_*mal 12 node.js gruntjs

grunt --version

grunt-cli v0.1.8
grunt v0.4.1
Run Code Online (Sandbox Code Playgroud)

$ npm -v

1.2.18
Run Code Online (Sandbox Code Playgroud)

$ node -v

v0.10.6
Run Code Online (Sandbox Code Playgroud)

当我跑来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.
Run Code Online (Sandbox Code Playgroud)

我搜索过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
Run Code Online (Sandbox Code Playgroud)

我可以将其中一个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/
Run Code Online (Sandbox Code Playgroud)

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

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.
Run Code Online (Sandbox Code Playgroud)

模式进展....

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.
Run Code Online (Sandbox Code Playgroud)

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

};
Run Code Online (Sandbox Code Playgroud)

Ewa*_*wan 13

我认为你在寻找什么是实际的命令行工具grunt-init中发现的工程脚手架文档.

您当前的命令grunt init正在查找其中Gruntfile.js的任务和任务init.它显然无法找到你Gruntfile.js所以抛出那个错误.

编辑:

您的新错误是因为您已经复制了一个Gruntfile.js文件而且该default任务(如果您想打开它,则位于底部附近)将调用这些模块.通常,您将使用类似的东西bower将它们安装到本地目录中.

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

grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']);
Run Code Online (Sandbox Code Playgroud)

这基本上是说:当我运行grunt在命令行,运行任务jshint,qunit,concatuglify按此顺序.这些任务将在之前的版本中指定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']
  }
}
Run Code Online (Sandbox Code Playgroud)

您应该阅读每个任务的文档,并确保了解您要通过的选项.

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

 <%= jshint.gruntfile %>
Run Code Online (Sandbox Code Playgroud)

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


WEF*_*EFX 13

我有同样的错误,结果我只需要先运行npm install.虽然我之前已经安装了npm,但是安装后某些东西已经损坏了.快速重新安装工作; 我需要的只是这两个命令:

npm install
grunt jshint
Run Code Online (Sandbox Code Playgroud)

  • 谢谢先生`npm install`让我摆脱了痛苦. (2认同)