加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

NPM 全局模块安装位置

发布时间:2023-05-16 14:19:40 所属栏目:教程 来源:
导读:首先你可以输入 npm config 命令,看看会输出什么内容:

$ npm config list
; cli configs
user-agent = "npm/3.10.10 node/v6.10.3 linux x64"

; userconfig /home/sitepoint/.npmrc
prefix = "/home/sit
首先你可以输入 npm config 命令,看看会输出什么内容:

$ npm config list
; cli configs
user-agent = "npm/3.10.10 node/v6.10.3 linux x64"
 
; userconfig /home/sitepoint/.npmrc
prefix = "/home/sitepoint/.node_modules_global"
 
; node bin location = /usr/bin/nodejs
; cwd = /home/sitepoint
; HOME = /home/sitepoint
; "npm config ls -l" to show all defaults.
npm config 命令提供了我们的安装的信息。现在,让我们获取它们的安装位置。

$ npm config get prefix
/usr
This is the prefix we want to change, so as to install global packages in our home directory. To do that create a new directory in your home folder.

$ cd ~ && mkdir .node_modules_global
$ npm config set prefix=$HOME/.node_modules_global
为了在我们的 home 目录安装全局模块,我们要更改的 prefix 就是这个,我们首先在 home 目录中创建一个新文件夹。

$ npm config get prefix
/home/sitepoint/.node_modules_global
$ cat .npmrc
prefix=/home/sitepoint/.node_modules_global
我们仍然在 root 用户目录下安装了 npm, 但是由于我们该改变了全局模块的安装位置,我们需要再一次安装 npm,这一次我们会安装最新版本的 npm5到新用户目录。

$ npm install npm --global
└─┬ npm@5.0.2
  ├── abbrev@1.1.0
  ├── ansi-regex@2.1.1
....
├── wrappy@1.0.2
└── write-file-atomic@2.1.0
最后我们需要添加 .node_modules_global/bin 至 $PATH 环境变量。

这样我们就可以在命令行运行这些全局模块。

在 .profile,.bash_profile 或者 .bashrc 文件中添加下面这一行,并重启你的终端。

`export PATH="$HOME/.node_modules_global/bin:$PATH"`
现在,命令行会首先发现 .node_modules_global/bin 目录,并且会使用正确的 npm 版本。

$ which npm
/home/sitepoint/.node_modules_global/bin/npm
$ npm --version
5.0.2

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章