Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

设置淘宝源的npm #11

Open
varHarrie opened this issue Feb 21, 2017 · 0 comments
Open

设置淘宝源的npm #11

varHarrie opened this issue Feb 21, 2017 · 0 comments
Labels
Milestone

Comments

@varHarrie
Copy link
Owner

varHarrie commented Feb 21, 2017

非常感谢淘宝提供的NPM镜像,使国内下载NPM包速度有了极大的提升。

为了便于使用,官方还提供了一个cnpm命令行工具,通过npm install -g cnpm --registry=https://registry.npm.taobao.org安装,变可以用cnpm代替npm安装node模块

但是,由于cnpm安装机制跟npm上有区别,会导致了诸如babel全家桶一类的包安装错误(不知道现在还有没有这个问题)

为了提高包安装速度,也尝试过使用前段时间Facebook推出的yarn,速度上确实有明显提升,功能上也几乎与npm等同。只可惜,处于安全考虑,它不支持一些安装过程中带执行脚步的包。依然不是完美的替代品

下面提供一种,既能保持完整的npm功能,又能保证国内下载速度等方法

npm的参数registry提供了更改下载源的方法,使用npm安装任意包的时候可以添加--registry=https://registry.npm.taobao.org,以切换到淘宝源,下载速度上跟cnpm无异。

但是,每次安装都需要添加一大段的命令,既难记又不方便。

在linux和macos中,可以通过alias添加别名命令:

alias cnpm="npm --registry=https://registry.npm.taobao.org"

在windows中并没有这样的方法,这里提供一种实现方法:

  1. 在任意目录下创建一个文件夹,用来存放下面三个文件,例如我创建了一个c:\bin目录
  2. 在该目录下建立文件aliases,内容如下
ls=dir /ONE $*
cd=cd /d $*
cnpm=npm $* --registry=https://registry.npm.taobao.org

这个文件用于定义别名命令,你也可以添加其他命令
3. 在该目录建立文件cmd_autorun.cmd,内容如下

@echo off
cls
doskey /macrofile=c:\bin\aliases

这个文件用于注册这些命令,注意将macrofile的路径改成你自己的
4. 在该目录下建立set_cmd_autorun.reg,内容如下

REGEDIT4

[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"Autorun"="c:\\bin\\cmd_autorun.cmd"

这个文件用于开机自动注册命令,注意将Autorun路径改成你自己的
5. 然后运行cmd_autorun.cmdset_cmd_autorun.reg文件
6. 之后再也不用管这些文件了,并且保证这些文件不会被删除

@varHarrie varHarrie added this to the Javascript/Node milestone Feb 21, 2017
@varHarrie varHarrie removed this from the Javascript/Node milestone Mar 1, 2017
@varHarrie varHarrie added NPM and removed Javascript labels Mar 1, 2017
@varHarrie varHarrie added this to the Posts milestone Aug 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant