npm/yeoman 在没有 sudo 的情况下安装 generator-angular [英] npm / yeoman install generator-angular without sudo

查看:31
本文介绍了npm/yeoman 在没有 sudo 的情况下安装 generator-angular的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Yo (来解决这个问题,你可以花几个小时虔诚地讨论它们的利弊.

我个人不喜欢将我的 user 软件包安装到 global /usr/ 文件夹中./usr/ 用于在计算机上的所有用户之间共享的软件.即使它只是使用机器,仍然有充分的理由尊重 Unix 文件系统层次结构的设计方式.例如,如果您决定在某一时刻擦除整个节点安装.

启用 npm 以全局安装软件包而不会脱离 $HOME 的首选方法是 设置本地节点前缀.这就像运行一样简单

echo 'prefix = ~/.node' >>~/.npmrc

在您的本地 shell 中.之后,您需要调整您的 $PATH,通过调整您最喜欢的 shell 的配置来指向全局节点可执行文件的新安装目标.例如.通过添加

export PATH="$PATH:$HOME/.node/bin"

到你的~/.bashrc.之后,您可以在没有 sudo 的情况下愉快地运行 npm install -g generator-angular,而不会遇到权限冲突,如果某些东西完全损坏并且您想从头开始,您需要做的就是删除你的 ~/.node 目录.

I tried to install generator-angularjs using Yo (Yoeman) without sudo:

npm install -g generator-angular

I get:

Error: EACCES, mkdir '/usr/lib/node_modules/generator-angular'

When I type in sudo yo, yo tells me that I should not use sudo (which is perfectly understandable).

I have a ~/node_modules directory - why doesn't yo install its packages there?

解决方案

Generators are designed to be installed globally. Otherwise, you always have to install the generator you're about to use in each project, which is unnecessarily painful. Also, you don't get to see the lovely yo menu which lists you all the available generators (unless of course, you install them all locally):

Setting up npm for global installation

So, how do we get npm to install packages globally? As you correctly said, you should never, ever run yo with sudo. There are lots of different solutions to this problem and you can spend hours discussing their pros and cons religiously.

I personally dislike installing my user packages into the global /usr/ folder. /usr/ is for software that is shared across all users on the computer. Even if it's only using the machine, there are still good reasons to respect the way the Unix file system hierarchy is designed. For example if you decide at one point to wipe your whole node installation.

My preferred way of enabling npm to install packages globally without breaking out of $HOME is to set a local node prefix. This is as easy as running

echo 'prefix = ~/.node' >> ~/.npmrc

in your local shell. After that, you want to adjust your $PATH, to point to the new installation destination for global node executables by adjusting your favorite shell's config. E.g. by adding

export PATH="$PATH:$HOME/.node/bin"

to your ~/.bashrc. After that, you can happily run npm install -g generator-angular without sudo, without running into permission conflicts and if something is completely broken and you want to start from scratch, all you need to do is remove your ~/.node directory.

这篇关于npm/yeoman 在没有 sudo 的情况下安装 generator-angular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆