致命错误:无法findGruntfile

咕噜 – 版本

grunt-cli v0.1.8 grunt v0.4.1 

$ npm -v

 1.2.18 

$ node -v

 v0.10.6 

当我运行grunt init创buildGruntfile.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. 

我search了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 

我可以只复制这些咕噜文件之一到/ ex / co / www / dev / htdocs / unittest(其中的Javascript / quinttesting)? 还是有什么我失踪?

我正试图在Centos 6.4上运行grunt。 另外,为什么grunt init创buildGruntfile.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()); }); }; 

我认为你要找的是在Project Scaffolding文档中find的实际的命令行工具grunt-init

你当前的命令grunt init正在寻找一个Gruntfile.js和它里面的任务init 。 显然无法find你的Gruntfile.js所以是抛出该错误。

编辑:

你的新错误是因为你已经复制了一个Gruntfile.js文件,并且default任务(如果你想打开它,发现在底部附近)将会调用这些模块。 通常情况下,您将使用像bower这样的东西安装到您的本地目录。

我上面提到的默认任务是这样的:

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

这基本上是说:当我在命令行运行grunt时,按顺序运行任务jshintqunitconcatqunit 。 这些任务将在Gruntfile被指定。 要了解该文件更多检查一个示例Gruntfile 。

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

编辑2:

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

Gruntfile.js你的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特定文件/属性的Gruntfile.js 。 在这种情况下,这来自watch任务,并指向jshint.gruntfile属性作为要观察的文件。

我有这个相同的错误,事实certificate我只是需要先运行npm install。 即使我以前已经安装了npm,但是在安装过程中,某些东西已经变得腐败了。 快速重新安装工作; 我所需要的只是这两个命令:

 npm install grunt jshint 

我有一个类似的问题,我使用下面的命令,它为我工作

 grunt -f grunt server 

希望它也适用于你。

检查你是否在你正在工作的dirctory上运行npm install那么它可能会解决你的问题