node.js 致命错误:无法找到 Gruntfile
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16611235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal error: Unable to find Gruntfile
提问by kamal
grunt --version
咕噜声——版本
grunt-cli v0.1.8
grunt v0.4.1
$ npm -v
$ npm -v
1.2.18
$ node -v
$节点-v
v0.10.6
When I run grunt initto create the Gruntfile.js, I get error:
当我运行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.
I have searched for Grunfile.js, and I get:
我搜索了 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
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?
我可以将这些 grunt 文件之一复制到 /ex/co/www/dev/htdocs/unittest (Javascript / quint 测试所在的位置)吗?或者还有什么我想念的吗?
I am trying to run grunt on Centos 6.4. Also, why doesn't grunt initcreate the Gruntfile.js?
我正在尝试在 Centos 6.4 上运行 grunt。另外,为什么不grunt init创建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:
我的 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());
});
};
回答by Ewan
I think what you're looking for is the actual command line tool grunt-initfound in the Project Scaffoldingdocumentation.
我认为您正在寻找的是grunt-init在Project Scaffolding文档中找到的实际命令行工具。
Your current command grunt initis looking for a Gruntfile.jsand the task initinside it. It is obviously unable to find your Gruntfile.jsso is throwing that error.
您当前的命令grunt init正在寻找 aGruntfile.js和其中的任务init。显然无法找到您的Gruntfile.js所以抛出该错误。
Edit:
编辑:
Your new error is because you have copied a Gruntfile.jsfile over and the defaulttask (found near the bottom if you want to open it) will be calling those modules. Normally you will have used something like bowerto install them into your local directory.
你的新错误是因为你复制了一个Gruntfile.js文件,default任务(如果你想打开它,可以在底部附近找到)将调用这些模块。通常,您会使用类似的bower方法将它们安装到本地目录中。
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 gruntat the command line, run the tasks jshint, qunit, concatand uglifyin that order. These tasks will have been specified earlier on in the Gruntfile. To understand the file more check out a sample Gruntfile.
这基本上是说:当我运行grunt在命令行,运行任务jshint,qunit,concat并uglify按此顺序。这些任务将在Gruntfile. 要更多地了解该文件,请查看示例 Gruntfile。
May I suggest taking a look at the Yeomanas it is a great tool to provide you with scaffolding for your apps (including a working Gruntfile.js).
我可以建议看看Yeoman,因为它是一个很好的工具,可以为您的应用程序(包括一个工作Gruntfile.js)提供脚手架。
Edit 2:
编辑2:
It is important you understand what is happening. The Gruntfile.jsyou have copied over looks to be calling "subgrunt" tasks. i.e. grunttasks in sub projects. Is this what you are wanting?
了解正在发生的事情很重要。在Gruntfile.js你复制看起来是叫“subgrunt”任务。即grunt子项目中的任务。这是你想要的吗?
Following through your Gruntfile.js- when you run gruntit is calling the following order: 'jshint', 'nodeunit', 'subgrunt'.
通过您的以下Gruntfile.js-当你运行grunt它在调用顺序如下:'jshint', 'nodeunit', 'subgrunt'。
Ideally you want to write this yourself rather than copying it as there are some options being called by jshintI am unfamiliar with. If you read the jshint documentationit doesn't mention the first three options.
理想情况下,您想自己编写它而不是复制它,因为jshint我不熟悉一些选项。如果您阅读jshint 文档,它不会提到前三个选项。
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 watchtask and points to the jshint.gruntfileproperty as a file to watch.
这是对Gruntfile.js. 在这种情况下,这来自watch任务并指向jshint.gruntfile属性作为要监视的文件。
回答by WEFX
I had this same error, and it turned out I just needed to run npm install first. Even though I had already installed npm previously, something had become corrupt w/ the installation. A quick re-install worked; All I needed were these two commands:
我有同样的错误,结果我只需要先运行 npm install 。尽管我之前已经安装了 npm,但安装时有些东西已经损坏了。快速重新安装工作;我只需要这两个命令:
npm install
grunt jshint
回答by TAHA SULTAN TEMURI
Grunt file should be in root directory not inside a folder somewhere, also it should spell "Gruntfile.js".
Grunt 文件应该在根目录中而不是在某个文件夹内,也应该拼写"Gruntfile.js"。
Example
例子
In case of asp.net Core angular project its location should be.
如果是 asp.net Core angular 项目,它的位置应该是。
It wont work if you put this file inside ClientApp or somewhere else until you define some method to do this.
如果你把这个文件放在 ClientApp 或其他地方,它就不会工作,直到你定义了一些方法来做到这一点。
回答by Abdul Hamid
I had a similar issue, I used the below commands and it worked for me
我有一个类似的问题,我使用了下面的命令,它对我有用
grunt -f
grunt server
Hope it works for you also.
希望它也适用于您。
回答by Sagar Shivsharan
check whether you are on dirctory that you are working on and run npm install there it will probably solve your problem
检查您是否在正在处理的目录中并在那里运行 npm install 它可能会解决您的问题


