npm 全局安装遇到权限问题怎么解决 #154
CongratulateWE
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
npm 的全局安装
全局安装可以让你在本地运行包里的代码,当作一个工具集来使用。
如果是npm5.2以上的版本,推荐使用 npx 来全局运行包,特别是这个命令只是偶尔使用的话。
安装方法是
npm install -g <package_name>
如果全局安装遇到了权限问题,推荐使用node版本管理工具重新安装npm,或者手动更改npm的默认文件夹。
参考: https://docs.npmjs.com/downloading-and-installing-packages-globally
使用 node version manager 解决全局安装问题
安装node官网给了两种方式:
官网推荐使用这种方式修改
参考:
https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally
安装n
参考 https://github.com/tj/n
安装之前不需要写在node,而且有node更好
sudo npm install -g n
不过上面这个方式只能让一个用户免去sudo执行命令,其他的用户还是需要使用sudo来执行,否则还是有权限的问题。
nvm 和 n 的区别
两者安装的node位置不同,n不论如何切换,node的路径都是 /usr/local/bin/node,这样也使得node全局安装的包是通用的。
nvm 安装在 ~/.nvm 目录下,不同版本的node在不同文件夹下,安装的包是不通用的。
参考: https://cloud.tencent.com/developer/article/1674774
Beta Was this translation helpful? Give feedback.
All reactions