Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to copy node_modules?

Tags:

node.js

If I want to copy a node project: Does it make any difference if I just copy node_modules or install all the modules again from scratch via npm?

like image 511
ben Avatar asked Jun 08 '14 00:06

ben


People also ask

Can I copy node_modules folder to another project?

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)

Are node modules safe?

Most node modules are open source and you can review their code in their repository (usually Github). So that's the best way to 'trust' them. Some node modules give you prebuilt native binaries, so that might be riskier in a way, but if it is popular (like ws for example) then I see no issue.

Can I delete node_modules and reinstall?

You could remove your node_modules/ folder and then reinstall the dependencies from package. json. This would erase all installed packages in the current folder and only install the dependencies from package.

Can we share node modules?

Option 1: Link to a Local Project Folder Once you've moved your shared code into a separate project, link the project as a dependency using npm link. Note: The shared library can be maintained in a separate repository or the same repository as your other projects (a.k.a, monorepo).


1 Answers

2017-05-12

I've updated this answer to reflect changes since the release of npm 3.x and new tools that are available.

npm v3 dependency installation is now non-deterministic meaning you may get different packages depending on the order in which packages have been installed over time. This isn't necessarily a bad thing, just something to be aware of.

Given this change I personally don't copy my node_modules directory around too much (it's still possible though!) and instead opt for a clean install most of the time.

There are new tools like Yarn Package Manager which can speed up the installation process if you are doing that a lot (but as of 2017-05-12 it's unclear how well it handles private npm organisations and private scoped packages).

So the takeaway is still pretty much the same: it won't hurt, but maybe err on the side of a clean install. If something weird does happen and you run into problems then you can just delete node_modules and run npm install.


Original answer from 2014-06-08:

In general it should be fine - I copy the node_modules directory sometimes from my other projects to speed up the setup process.

You can always copy node_modules and then run npm install or npm update in the new project to make sure you've got up-to-date versions. npm will use the files in node_modules as a cache and should only bring down newer content if required.

In short: it won't hurt. If something weird does happen and you run into problems then you can just delete node_modules and run npm install.

like image 196
Sly_cardinal Avatar answered Sep 22 '22 14:09

Sly_cardinal