首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >致命错误:找不到Gruntfile

致命错误:找不到Gruntfile
EN

Stack Overflow用户
提问于 2013-05-17 22:13:51
回答 5查看 60.5K关注 0票数 14

grunt --版本

代码语言:javascript
复制
grunt-cli v0.1.8
grunt v0.4.1

$ npm -v

代码语言:javascript
复制
1.2.18

$ node -v

代码语言:javascript
复制
v0.10.6

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

代码语言:javascript
复制
$ 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,得到的结果是:

代码语言:javascript
复制
/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

代码语言:javascript
复制
cp /root/.npm/grunt/0.4.1/package/Gruntfile.js /ex/co/www/dev/htdocs/unittest/

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

代码语言:javascript
复制
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.

模式进度...

代码语言:javascript
复制
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:

代码语言:javascript
复制
/*
 * 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());
  });

};
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-05-17 22:32:23

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

您当前的命令grunt init正在查找Gruntfile.js及其内部的任务init。它显然无法找到您的Gruntfile.js,因此抛出该错误。

编辑:

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

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

代码语言:javascript
复制
grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']);

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

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

编辑2:

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

跟随你的Gruntfile.js -当你运行grunt时,它会调用下面的命令:'jshint', 'nodeunit', 'subgrunt'

理想情况下,您希望自己编写这段代码,而不是复制它,因为有一些选项是由我不熟悉的jshint调用的。如果你阅读jshint documentation,它没有提到前三个选项。

我认为你应该试试这样的东西:

代码语言:javascript
复制
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']
  }
}

您应该通读每个任务的文档,并确保您理解正在传递的选项。

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

代码语言:javascript
复制
 <%= jshint.gruntfile %>

这是对Gruntfile.js中特定文件/属性集的引用。在本例中,它来自watch任务,并将jshint.gruntfile属性作为要监视的文件指向。

票数 13
EN

Stack Overflow用户

发布于 2014-06-30 23:14:29

我遇到了同样的错误,原来我只需要先运行npm install即可。尽管我之前已经安装了npm,但在安装过程中有些东西已经损坏了。快速重新安装起作用了;我所需要的只是这两个命令:

代码语言:javascript
复制
npm install
grunt jshint
票数 14
EN

Stack Overflow用户

发布于 2014-07-31 19:27:41

我遇到了类似的问题,我使用了下面的命令,它对我很有效

代码语言:javascript
复制
grunt -f 
grunt server

希望它也适用于你。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16611235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档