2

I had some issues with a current iOS React Native project that i spent a long time working on. I've decided to start afresh. However i did spend a lot of time ensuring i got the correct versions of various react native modules and configuring these to work. These sit in my old 'node_modules' folder.

Can i copy these modules/folders (from the old 'node_modules') over to my new project? Do i need to update 'package.json' or link using 'react-native link xxxxx'? It it as simple as that in theory?

chai86
  • 425
  • 2
  • 14
  • 34
  • 2
    Not sure I understand your problem correctly, but theorically all you have to do is copy/paste your package.json in your fresh project and run `npm install`. – Bernard Pagoaga Jul 02 '19 at 13:54

3 Answers3

10

You don't need to copy over your node_modules directory. You can if you'd like, but it is not considered best practice. In any case, you shouldn't make any modifications to the files inside node_modules.

Preferably you need to only copy over your package.json file and optionally, your package-lock.json (or yarn.lock if you're using Yarn) file so that your project will be easily installable and upgradeable on other computers.

When you have a package.json or package-lock.json file, you can run npm install (or yarn install) to install the packages to your node_modules directory.

Copying your package-lock.json file over as well would ensure that the exact same versions of all of the packages that you have installed in your previous project will be installed in the new project as well. See this for more information on the package-lock.json file.

Unfortunately, I don't know much about react-native and linking react-native dependencies, but from this answer it seems that you would have to link any dependencies that contain native code again after you've run npm install.

D Malan
  • 10,272
  • 3
  • 25
  • 50
  • ok. so i don't need to copy over the package folder, you're saying by copying over my yarn.lock file and running 'yarn add react-native-module-xx' it will fetch and install these? – chai86 Jul 02 '19 at 14:12
  • 1
    If you copy over your `yarn.lock` file with all the dependencies that you need, you only need to run `yarn install` and all of the dependencies will be installed. No need to run `yarn add ` for each package. – D Malan Jul 02 '19 at 14:29
3

No, I wouldn't recommend you to copy specific module from node_modules folder as once installed it has entries in .bin folder and files which you will miss while copying and it will be of no help in new project as they will be downloaded and installed again due to missing indexes.

Solutions :

  • You can use same package.json and package-lock in another project if you are sure that the dependencies version in it are exactly compatible or one you want, and install those dependencies in new project. The package-lock.json will ensure the version you choose.

  • Yes you can copy whole node_modules (have done it multiple times) from one project to another and use same package.json and package-lock (Will only save time in dependencies installation/download)

As you are starting fresh what i will recommend is create a new package.json and copy only those dependencies which are important for the project initialization, and as you progress add specific dependencies you need while developing. This will save you from huge garbage of unused dependencies which are hard to keep track of once the project inflates.

Vikas Saini
  • 444
  • 2
  • 11
  • Hi Vikas, so my new package.json file is pretty empty, and from my old package.json file, i know i have 8 dependencies to add. So can i copy for example 'react-native-fetch-blob' folder, from the old node_modules to the new node_modules folder and just run 'npm install react-native-fetch-blob'? – chai86 Jul 02 '19 at 14:09
  • 1
    You can copy but it will be of no use, its as simple as copy the package.json entry for "react-native-fetch-blob" and do npm install. Also you can do "npm install react-native-fetch-blob@" if you want a specific version else latest will be installed. – Vikas Saini Jul 02 '19 at 14:21
  • 1
    Not necessarly, once version are matched then just remove the ^ from the package.json "react-native-fetch-blob" and "react-native" versions. You won't have to link again and again after ever npm install, its all about compatibility. :-) – Vikas Saini Jul 02 '19 at 14:27
  • right now there's quite a few ^ in the package.json. Should they be removed before the npm install? – chai86 Jul 02 '19 at 14:30
  • 1
    package-lock take cares of this, but if you want to be sure from your side and not sharing the package-lock with other contributors then you should remove the ^ only from the link dependencies. Note : As you progress and decide to upgrade to later version of react then keep in mind that you should update these link manually and link again (You might want to upgrade later as newer libraries require newer version of framework). ^ just indicates to the npm that install latest/better version if available. – Vikas Saini Jul 02 '19 at 14:41
1

Maybe you can move it but you can't copy it, at least not with a naive cp -r! The node_modules/.bin directory ought to be full of symlinks to scripts in their respective directories in node_moudles, but after a cp -r these will be copies, which will now resolve relative module paths incorrectly.

For example, npm install typescript installs a executable script with the contents

#!/usr/bin/env node
require('../lib/tsc.js')

in two locations. Before a copy, that looks like this:

$ ls -l node_modules/.bin/tsc node_modules/typescript/bin/tsc
-rwxr-xr-x 45 node_modules/.bin/tsc
-rwxr-xr-x 45 node_modules/typescript/bin/tsc

and after a cp -r copy, like this:

$ ls -l node_modules/.bin/tsc node_modules/typescript/bin/tsc
lrwxr-xr-x 21 node_modules/.bin/tsc -> ../typescript/bin/tsc
-rwxr-xr-x 45 node_modules/typescript/bin/tsc

which makes the ../typescript/bin/tsc no longer exist, resulting in

$ tsc -b
...
Error: Cannot find module '../lib/tsc.js'
...
Thomas
  • 6,515
  • 1
  • 31
  • 47