如何在没有 root 访问权限的情况下设置 npm(节点包管理器)?
将 npm 设置为 root 用户非常简单且有效。除非您必须以 root 身份运行 npm 命令(不推荐)。所以我想我应该尝试将其设置为非 root 用户。
根据 npm 文档,没有 root 访问权限的非 root 用户可以通过以下方式设置 npm:
- 使用
root
、binroot
创建一个.npmrc
文件,manroot
指向用户拥有的文件夹。 - 然后运行安装脚本。
好的。安装很好。
但是node看不到npm提供的包。
那么如何让node知道npm提供的包呢? (当我之前以 root 身份安装 npm 时,我不需要执行任何操作)。 我可以在节点内设置 require.paths
,或者设置 NODE_PATH
环境变量,但是设置什么呢?
谢谢。
Setting npm up as the root user is straighforward and workds. Except you have to run npm commands as root (not recommended). So I thought I'd try setting it up as a non-root user.
According to npm documentation, a non-root user without root access can set up npm by:
- creating a
.npmrc
file withroot
,binroot
, andmanroot
pointing to folders that the user owns. - Then running the install script.
OK. Install was fine.
But node can't see the packages provided by npm.
So how do I make node aware of the packages provided by npm? (I didn't have to do anything when I previously installed npm as root).
I can set require.paths
within node, or set the NODE_PATH
environment variable, but to what?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我有用:
创建一个
~/.node
文件夹编辑
~/.npmrc
并添加行Edit your
~/.profile
或~/.bash_profile并添加这些行
现在我可以执行诸如
npm -g install 之类的操作http-server
它将安装到~/.node
无需 root。完成此操作后,当我输入http-server
时,它就会运行。This worked for me:
Make a
~/.node
folderEdit
~/.npmrc
and add the lineEdit your
~/.profile
or~/.bash_profile
and add these linesNow I can do things like
npm -g install http-server
and it will install to~/.node
without root. With this in place, when I then typehttp-server
, it runs.经过一番探索,您似乎可以:
NODE_PATH
设置为 npm 的根目录,或者require.paths.push('path_to_npm_root')
Through a little exploration, it seems you can either:
NODE_PATH
to whatever npm's root directory is, orrequire.paths.push('path_to_npm_root')