node.js 为什么 Grunt 会失败?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14695845/
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
Why does Grunt fail?
提问by Keva161
I'm getting a weird error after running npm run-script gruntin which it tell's me that node_modules/.bin/gruntfails.
运行后我收到一个奇怪的错误npm run-script grunt,它告诉我node_modules/.bin/grunt失败了。
I'm following a tutorial as im pretty new to Backbone (http://dailyjs.com/2012/11/29/backbone-tutorial-1/),
我正在学习一个教程,因为我对 Backbone 很陌生(http://dailyjs.com/2012/11/29/backbone-tutorial-1/),
Here's my package.jsonfile.
这是我的package.json文件。
{
"name": "btask"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"requirejs": "latest"
, "connect": "2.7.0"
}
, "devDependencies": {
"mocha": "latest"
, "chai": "latest"
, "grunt": "latest"
, "grunt-exec": "latest"
}
, "scripts": {
"grunt": "node_modules/.bin/grunt"
}
}
And here's a full transcript of the error.
这是错误的完整记录。
0 info it worked if it ends with ok
1 verbose cli [ 'node',
1 verbose cli '/Users/Keva161/.nvm/v0.8.18/bin/npm',
1 verbose cli 'run-script',
1 verbose cli 'grunt' ]
2 info using [email protected]
3 info using [email protected]
4 verbose read json /Users/Keva161/Documents/Web Dev/Webapps/Node/btask/package.json
5 verbose run-script [ 'pregrunt', 'grunt', 'postgrunt' ]
6 info pregrunt [email protected]
7 info grunt [email protected]
8 verbose unsafe-perm in lifecycle true
9 silly exec sh "-c" "node_modules/.bin/grunt"
10 silly sh,-c,node_modules/.bin/grunt,/Users/Keva161/Documents/Web Dev/Webapps/Node/btask spawning
11 info [email protected] Failed to exec grunt script
12 error [email protected] grunt: `node_modules/.bin/grunt`
12 error `sh "-c" "node_modules/.bin/grunt"` failed with 2
13 error Failed at the [email protected] grunt script.
13 error This is most likely a problem with the btask package,
13 error not with npm itself.
13 error Tell the author that this fails on your system:
13 error node_modules/.bin/grunt
13 error You can get their info via:
13 error npm owner ls btask
13 error There is likely additional logging output above.
14 error System Darwin 12.2.1
15 error command "node" "/Users/Keva161/.nvm/v0.8.18/bin/npm" "run-script" "grunt"
16 error cwd /Users/Keva161/Documents/Web Dev/Webapps/Node/btask
17 error node -v v0.8.18
18 error npm -v 1.2.4
19 error code ELIFECYCLE
20 verbose exit [ 1, true ]
回答by arunkjn
The tutorial that you are talking about uses grunt 0.3. Grunt has updated since then and if you use "grunt": "latest" in your package.jsonfile then you will get grunt 0.4 by default.
您正在谈论的教程使用 grunt 0.3。Grunt 从那时起就进行了更新,如果您在package.json文件中使用 "grunt": "latest"那么默认情况下您将获得 grunt 0.4。
You can go through the migration guideto get things working.
您可以通过迁移指南来使工作正常进行。
In order to just get grunt working for the tutorial you can do this -
为了让咕噜声为教程工作,你可以这样做 -
- Install grunt-cli globally
npm install -g grunt-cli - Install grunt-init globally
npm install -g grunt-init - Install grunt locally (already installed if you followed the tutorial)
- Change
"grunt": "node_modules\\.bin\\grunt"in package.jsonto"grunt": "grunt" - Rename grunt.jsin root directory to Gruntfile.js
- Change
grunt.registerTask('default', 'exec copy-require');in Gruntfile.jstogrunt.registerTask('default', ['exec','copy-require']); - Run
npm run-script gruntas usual in command-line. Things should work fine now.
- 全局安装 grunt-cli
npm install -g grunt-cli - 全局安装 grunt-init
npm install -g grunt-init - 在本地安装 grunt(如果你按照教程已经安装了)
- 更改
"grunt": "node_modules\\.bin\\grunt"中的package.json来"grunt": "grunt" - 重命名grunt.js在根目录下Gruntfile.js
- 变化
grunt.registerTask('default', 'exec copy-require');在Gruntfile.js到grunt.registerTask('default', ['exec','copy-require']); - 运行
npm run-script grunt在命令行如常。现在一切正常。
You will get a build directory with r.js build.
您将获得一个带有 r.js 构建的构建目录。
If you still cannot get it working somehow with the above steps, you can change grunt: "latest"to grunt : "0.3"in your package.jsonfile and run npm installin shell.
如果您仍然无法通过上述步骤以某种方式使其工作,您可以在package.json文件中更改grunt: "latest"为并在 shell 中运行。grunt : "0.3"npm install
回答by owyongsk
arunkjn's answer will solve the problem. Though in accordance to the tutorial's intention, the author did not want to install grunt globally, which is also what I'm going for. Thanks to arunkjn, I figured that you could also:
arunkjn的回答将解决问题。虽然按照教程的意图,作者不想全局安装grunt,这也是我要的。感谢 arunkjn,我想你还可以:
- Install grunt-cli locally by
npm install grunt-cli - Rename grunt.js in root directory to Gruntfile.js
- Change
grunt.registerTask('default', 'exec copy-require');in Gruntfile.js togrunt.registerTask('default', ['exec','copy-require']);
- 通过本地安装 grunt-cli
npm install grunt-cli - 将根目录下的 grunt.js 重命名为 Gruntfile.js
- 将
grunt.registerTask('default', 'exec copy-require');Gruntfile.js更改为grunt.registerTask('default', ['exec','copy-require']);
And do npm run-script gruntagain and it should work.
再做npm run-script grunt一次,它应该可以工作。
回答by Jon Jaques
I've been using grunt and npm for a while now, and I've never used run_script. Maybe try this?
我已经使用 grunt 和 npm 有一段时间了,但我从未使用过run_script. 也许试试这个?
The scriptsportion is usually for running on a remote server, (ie Heroku or Travis)
该scripts部分通常用于在远程服务器上运行,(即 Heroku 或 Travis)
Remove the scriptsblock.
移除scripts块。
Then do
然后做
npm install -g grunt
May have to restart terminal after this.
在此之后可能必须重新启动终端。
cd myproject
npm install
grunt mytask

