React Native 初始化项目慢的解决办法

初始化React Native:react-native init ProjectName

由于墙的原因,即便开ss也会经常卡着不动。再次感谢方教授!

先看下NMP官网对 node-gyp 的介绍

node-gyp

Node.js native addon build tool

node-gyp is a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js. It >bundles the gyp project used by the Chromium team and takes away the pain of dealing with the various differences in build platforms. It is the replacement to the node-waf program which is removed for node v0.8. If you have a native addon for node that still has a wscript file, then you should definitely add a binding.gyp file to support the latest versions of node.

在初始化的过程中,npm 会下载源码然后使用 node-gyp 进行编译,而 node-gyp 编译时候需要 NodeJs 源码来提供头文件,所以它会先尝试下载 NodeJs 源码,而在天朝墙外 CDN 经常抽风,下载 NodeJs 源码奇慢无比,那么自然要卡很久。

解决方法:先把 NodeJs 源码下载到本地,然后提取给 node-gyp。

terminal下( nodejs 及 wget 自行安装 ):

touch node-gyp.sh

vi node-gyp.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# nodejs 版本号
NODE_VERSION=`node -v | cut -d'v' -f 2`

echo ${NODE_VERSION}

# 下载源码包(淘宝镜像)
wget http://npm.taobao.org/mirrors/node/v$NODE_VERSION/node-v$NODE_VERSION.tar.gz

# 删除现有内容不完整的目录
rm -rf ~/.node-gyp
mkdir ~/.node-gyp

# 解压缩并重命名到正确格式
tar zxf node-v$NODE_VERSION.tar.gz -C ~/.node-gyp
mv ~/.node-gyp/node-v$NODE_VERSION ~/.node-gyp/$NODE_VERSION

# 创建一个标记文件
printf "9\n">~/.node-gyp/$NODE_VERSION/installVersion

保存退出后执行 bash node-gyp.sh

最后再替换 npm 淘宝镜像

npm config set registry=http://registry.npm.taobao.org/

这样 React Native初始化会快很多。

Author

Ludis

Posted on

2016-08-03

Updated on

2016-08-03

Licensed under

Comments