How to use react-native with yarn?

Update: As predicted, Yarn is included in React Native 0.37 by default. This article is now outdated.
As soon as I heard about yarn, I was like: “Faster react-native init? Let’s do it!”.

So I went on and got those results:

react-native init (npm): 3"57s
react-native init (yarn): 2"22s

Yay!

So here how you can do it:

  1. Find where the `react-native-cli` is installed by typing in your terminal:
    npm config get prefix

    It will give you a path

  2. “cd” to this path and add the following
    cd <path>/lib/node_modules/react-native-cli
  3. Then save the original index before modifying it:
    cp index.js index_ori.js
  4. Edit the file:
    vim index.js
  5. Change the followings npm command to yarn in the #run and #runVerbose methods

Basically, you need to:

  • switch “npm install” by “yarn add”,
  • remove “–save” and “–save-exact”

line 216

exec('npm install --save --save-exact ' + getInstallPackage(rnPackage), function(e, stdout, stderr) {

to

exec('yarn add ' + getInstallPackage(rnPackage), function(e, stdout, stderr) {

line 232

var proc = spawn('npm', ['install', '--verbose', '--save', '--save-exact', getInstallPackage(rnPackage)], {stdio: 'inherit'});

to

var proc = spawn('yarn', ['add', '--verbose', getInstallPackage(rnPackage)], {stdio: 'inherit'});

Now enjoy your

react-native init FasterProject

CAUTION: Make sure to keep the original one. It is quite sure that the react-native OSS team will include this capability in the core later. But like me, you couldn’t wait.

3 thoughts on “How to use react-native with yarn?

  1. Pingback: How to use react-native with yarn? – Kryptonik – ReactJS News

Leave a Reply

Your email address will not be published. Required fields are marked *