npm yarn 切换安装源或使用代理进行安装

2021/5/14 11:09:15npm_yarnnpm yarn

正常的安装:
npm的几种安装方式:官方教程:https://docs.npmjs.com/

npm install moduleName # 安装模块到项目目录下
npm install -g moduleName # -g 的意思是将模块安装到全局,具体安装到磁盘哪个位置,要看 npm config prefix 的位置。
npm install --save moduleName # --save 的意思是将模块安装到项目目录下,并在package文件的dependencies节点写入依赖。
npm install --save-dev moduleName # --save-dev 的意思是将模块安装到项目目录下,并在package文件的devDependencies节点写入依赖。
npn -i -S moduleName # -i是install的缩写,-S是--save的缩写。

# -i 是install的缩写
# -S 是--save的缩写
# -D 是--save-dev的缩写

yarn的几种安装方式:官方教程:https://yarn.bootcss.com/docs/cli/add/

yarn add package-name
yarn add file:/path/to/local/folder
yarn add file:/path/to/local/tarball.tgz
yarn add link:/path/to/local/folder
yarn add <git remote url>
yarn add <git remote url>#<branch/commit/tag>
yarn add https://my-project.org/package.tgz

一,使用cnpm

$ npm install cnpm -g --registry=https://registry.npm.taobao.org
$ cnpm install xxx

上面这一步是个伪命题,npm源地址都请求不到,怎么还能安装cnpm(以上是网上的教程)。

二,临时指定镜像源地址进行安装:

npm的设置方式:

npm install xxx --registry=https://registry.npm.taobao.org

yarn的设置方式:

yarn global xxx  --registry=https://registry.npm.taobao.org

三,持久性切换镜像源地址:

npm的设置方式:

npm config set registry https://registry.npm.taobao.org

yarn的设置方式:

yarn config set registry https://registry.npm.taobao.org

也可以使用nrm来管理持久性的镜像地址列表:nrm——npm镜像管理工具使用入门

二,npm使用代理进行安装:

设置代理

npm config set proxy="http://127.0.0.1:1080"

https代理:

npm config set https-proxy http://127.0.0.1:1080

设置带有用户名和密码的代理:

npm config set proxy http://username:password@server:port
npm confit set https-proxy http://username:password@server:port

取消代理:

npm config delete proxy
npm config delete https-proxy

yarn使用代理进行安装:

yarn config set proxy http://127.0.0.1:1080
yarn confit set https-proxy http://127.0.0.1:1080

yarn config set proxy http://username:password@server:port
yarn confit set https-proxy http://username:password@server:port

取消代理:

yarn config delete proxy
yarn config delete https-proxy