如何npm不以root身份安装全局? [英] How to npm install global not as root?
问题描述
我在没有root访问权限的unix框中.
I'm on a unix box where I don't have root access.
我将我的.npmrc文件(在用户的根目录中)更改为:
I changed my .npmrc file (in my user's root directory) to:
prefix=~/global_npm
现在,当我执行"npm install -g软件包名称"时,它将安装在我的global_npm目录中.哪个好 然后,我通过以下方式更新自己的.bashrc文件,从而获得了对其的路径访问权限:
Now when I do "npm install -g packagename" it installs inside my global_npm directory. Which is good. And then I gave myself path access to it by updating my .bashrc file with:
export PATH=$PATH:~/global_npm/bin
我还需要做其他事情吗?我想我需要设置NODE_PATH,但不确定吗?
Do I need to do anything else? I think I need to set NODE_PATH but I'm not sure?
推荐答案
Sindre Sorhus has a great guide at github.com/sindresorhus/guides which I've reposted here.
npm默认情况下在您的项目中本地安装软件包.您还可以全局安装软件包(例如npm install -g <package>
)(对于命令行应用程序有用).但是,这样做的缺点是您必须是root用户(或使用sudo
)才能全局安装.
npm installs packages locally within your projects by default. You can also install packages globally (e.g. npm install -g <package>
) (useful for command-line apps). However the downside of this is that you need to be root (or use sudo
) to be able to install globally.
这是一种为给定用户全局安装软件包的方法.
Here is a way to install packages globally for a given user.
mkdir "${HOME}/.npm-packages"
2.引用此目录以供将来在.bashrc
/.zshrc
中使用:
2. Reference this directory for future usage in your .bashrc
/.zshrc
:
NPM_PACKAGES="${HOME}/.npm-packages"
3.指示npm
在哪里存储全局安装的软件包.在您的$HOME/.npmrc
文件中添加:
3. Indicate to npm
where to store your globally installed package. In your $HOME/.npmrc
file add:
prefix=${HOME}/.npm-packages
4.确保node
将找到它们.将以下内容添加到您的.bashrc
/.zshrc
:
4. Ensure node
will find them. Add the following to your .bashrc
/.zshrc
:
NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
5.确保您将找到已安装的二进制文件和手册页.将以下内容添加到您的.bashrc
/.zshrc
:
5. Ensure you'll find installed binaries and man pages. Add the following to your .bashrc
/.zshrc
:
PATH="$NPM_PACKAGES/bin:$PATH"
# Unset manpath so we can inherit from /etc/manpath via the `manpath`
# command
unset MANPATH # delete if you already modified MANPATH elsewhere in your config
MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
查看 npm-g_nosudo ,以自动完成上述步骤
Check out npm-g_nosudo for doing the above steps automagically
注意::如果您正在运行OS X,则.bashrc
文件可能不存在,并且终端将从另一个文件(例如.profile
或.bashrc
文件:
NOTE: If you are running OS X, the .bashrc
file may not yet exist, and the terminal will be obtaining its environment parameters from another file, such as .profile
or .bash_profile
. These files also reside in the user's home folder. In this case, simply adding the following line to them will instruct Terminal to also load the .bashrc
file:
source ~/.bashrc
这篇关于如何npm不以root身份安装全局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!