bash 如何在不出错的情况下安装 grunt-cli?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20555605/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:59:16  来源:igfitidea点击:

How can I install the grunt-cli without getting errors?

node.jsbashgruntjs

提问by Bryce Johnson

Inspired by Chris Coyier's post, I decided I'd give grunt a go. But I'm having big problems getting set up.

受到 Chris Coyier帖子的启发,我决定尝试一下 grunt。但是我在设置时遇到了大问题。

First, I installed Node.

首先,我安装了 Node。

Then I added a package.json file to my project root, including this:

然后我在我的项目根目录中添加了一个 package.json 文件,包括:

{
  "name": "example-project",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "~0.4.1"
  }
}

Then I ran npm install

然后我跑了 npm install

Finally, I ran npm install -g grunt-cli

最后,我跑了 npm install -g grunt-cli

which came back with a ton of errors:

返回了大量错误:

npm http GET https://registry.npmjs.org/grunt-cli
npm http 304 https://registry.npmjs.org/grunt-cli
npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli'
npm ERR!  { [Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/usr/local/lib/node_modules/grunt-cli',
npm ERR!   fstream_type: 'Directory',
npm ERR!   fstream_path: '/usr/local/lib/node_modules/grunt-cli',
npm ERR!   fstream_class: 'DirWriter',
npm ERR!   fstream_stack: 
npm ERR!    [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23',
npm ERR!      '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR!      'Object.oncomplete (fs.js:107:15)' ] }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Darwin 11.4.2
npm ERR! command "node" "/usr/local/bin/npm" "install" "-g" "grunt-cli"
npm ERR! cwd /Users/bryce/repo
npm ERR! node -v v0.10.23
npm ERR! npm -v 1.3.17
npm ERR! path /usr/local/lib/node_modules/grunt-cli
npm ERR! fstream_path /usr/local/lib/node_modules/grunt-cli
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli'
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/bryce/repo/npm-debug.log
npm ERR! not ok code 0
Bryce:repo bryce$ npm install -g grunt-cli
npm http GET https://registry.npmjs.org/grunt-cli
npm http 304 https://registry.npmjs.org/grunt-cli
npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli'
npm ERR!  { [Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/usr/local/lib/node_modules/grunt-cli',
npm ERR!   fstream_type: 'Directory',
npm ERR!   fstream_path: '/usr/local/lib/node_modules/grunt-cli',
npm ERR!   fstream_class: 'DirWriter',
npm ERR!   fstream_stack: 
npm ERR!    [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23',
npm ERR!      '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR!      'Object.oncomplete (fs.js:107:15)' ] }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Darwin 11.4.2
npm ERR! command "node" "/usr/local/bin/npm" "install" "-g" "grunt-cli"
npm ERR! cwd /Users/bryce/repo
npm ERR! node -v v0.10.23
npm ERR! npm -v 1.3.17
npm ERR! path /usr/local/lib/node_modules/grunt-cli
npm ERR! fstream_path /usr/local/lib/node_modules/grunt-cli
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli'
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/bryce/repo/npm-debug.log
npm ERR! not ok code 0

I assume the above errors are the reason that when I tried to run grunt, I simply get -bash: grunt: command not found

我认为上述错误是当我尝试运行时grunt,我只是得到的原因-bash: grunt: command not found

Anyone out there know what's happening?

有大佬知道怎么回事吗?

回答by Bryce Johnson

All it took was adding 'sudo', since I didn't have permission to install the grunt-cli by default. Once I ran the following command and entered my password, it worked as expected.

所需要的只是添加“sudo”,因为我没有默认安装 grunt-cli 的权限。一旦我运行了以下命令并输入了我的密码,它就会按预期工作。

sudo npm install -g grunt-cli

Hope this helps someone!

希望这对某人有帮助!

回答by srquinn

EDIT: The article quoted below is quite old and refers to pre-v0.3 Yikes!Currently, if npmis run with sudorights it will downgrade to the nobody user before executing commands.

编辑:下面引用的文章很旧,指的是 v0.3 之前的Yikes!目前,如果npmsudo权限运行,它将在执行命令之前降级为 nobody 用户。

According to the maintainer of npm, installing packages with sudois considered bad practice because you are allowing that package to have complete control of your system and you can't and SHOULDN'T trust these packages with root access.

根据 的维护者的说法npm,安装软件包sudo被认为是不好的做法,因为您允许该软件包完全控制您的系统,并且您不能也不应该信任这些具有 root 访问权限的软件包。

http://howtonode.org/introduction-to-npm

http://howtonode.org/introduction-to-npm

回答by mike-pz

Since you should not install using sudo - you will do this before installing packages and no longer will hit permission errors:

由于您不应该使用 sudo 进行安装 - 您将在安装软件包之前执行此操作,并且不再会遇到权限错误:

sudo chown -R $USER /usr/local

须藤 chown -R $USER /usr/local

source: https://howtonode.org/introduction-to-npm

来源:https: //howtonode.org/introduction-to-npm

回答by philidem

I would recommend installing grunt within your project (instead of using the -g option).

我建议在您的项目中安装 grunt(而不是使用 -g 选项)。

For example, to install the Grunt command line tools use:

例如,要安装 Grunt 命令行工具,请使用:

cd ~/path-to-my-project
npm install grunt-cli --save-dev

If you want to run grunt then you use:

如果你想运行 grunt,那么你可以使用:

cd ~/path-to-my-project
./node_modules/./bin/grunt

By installing grunt-cli within your project, you'll have full control over which version gets installed and you don't have to rely on inconsistent PATH variables.

通过在项目中安装 grunt-cli,您可以完全控制安装哪个版本,而不必依赖不一致的 PATH 变量。

回答by lundybuilds.it

I was having this issue as well, and after reading through the output a little closer i realized that my installed version of node.js was not compatible with the current version of grunt-cli.

我也遇到了这个问题,在仔细阅读输出后,我意识到我安装的 node.js 版本与当前版本的 grunt-cli 不兼容。

Updated node.js problem solved.

更新的 node.js 问题解决了。

回答by Naser

The command for installing grunt in your project folder is :

在项目文件夹中安装 grunt 的命令是:

npm install grunt --save-dev