最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can i move my node_modules folder to another project? - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question asked Jul 2, 2019 at 13:47 chai86chai86 4434 gold badges15 silver badges38 bronze badges 1
  • 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 Commented Jul 2, 2019 at 13:54
Add a ment  | 

3 Answers 3

Reset to default 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 puters.

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.

No, I wouldn't remend 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 patible 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 remend 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.

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'
...
发布评论

评论列表(0)

  1. 暂无评论