Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access node_modules from another folder

Tags:

node.js

npm

gulp

Recently started working with Gulp and I can't figure out is it really necessary to have a copy of node_modules directly in folder with current project? E.g. I have this structure:

mysite
└─builder
      └──node_modules
└─work
└─work2

How can I access node_modules in folder 'builder' from folder 'work' or 'work2' without copying it? It is quite large, about 100mb, and seems to me it has no sense to have a copy of it for every new project.

I tried this line export NODE_PATH='D:\OpenServer\domains\mysite\build' in file package.json and then tried command gulp but it replied
[10:24:27] Local gulp not found in d:\OpenServer\domains\mysite\work [10:24:27] Try running: npm install gulp

like image 825
Julia Avatar asked Apr 08 '16 04:04

Julia


People also ask

Can I copy node_modules folder to another project?

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@<version>" if you want a specific version else latest will be installed.

CAN node modules be shared?

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).

Where can I find node_modules folder?

On Unix systems they are normally placed in /usr/local/lib/node or /usr/local/lib/node_modules when installed globally. If you set the NODE_PATH environment variable to this path, the modules can be found by node. Non-global libraries are installed the node_modules sub folder in the folder you are currently in.


1 Answers

Short answer

Don't do it. Let NPM work the way it's designed to. However, to save space, you can delete the node_modules folder on projects that are currently dormant, and recreate it with a single shot of npm install when you switch back to them.


Justification

Even if you share your node_modules, you'll probably have redundancies in it anyway. What will you do about them next ?

It is the essence of NPM to replicate modules per project. If you dig into the node_modules folder tree, you may notice that it can even contain several replications of a same library under one given dependencies tree. Say you requested two modules explicitely, and both these modules themselves pulled a dependency that takes care of a lot of things, and is therefore called lib_DADDYMUMMY :

node_modules
    + a_module_you_use v0.5
        + lib_DADDYMUMMY v0.1 (pulled as a dependency of this module)
    + another_module_that_you_requested v0.3
        + lib_DADDYMUMMY v0.1 (again ! pulled as a dependency of this other module)

This comes in handy when your two module start needing different versions of lib_DADDYMUMMY. This comes in handy when you maintain long-lived projects ! And hell knows that in the JavaScript world, with fast changing APIs, you can consider most any decent project as long-lived. :)

One could imagine having all dependencies being shared by everyone, living in a flat structure, with several versions of a library living next to each other and every one finding what he needs there. That repository could be called, say, .m2. But that's just not the way NPM works unfortunately.

NPM considers that storage space is cheap. That's its price for helping you manage versions in dependencies, dependencies of dependencies, and dependencies of dependencies of dependencies. I consider that it's an affordable price for taking care of the dirty jobs the day when work and work2, as their lives go on, take diverging maintenance paths. I wouldn't try getting in its way by forcing a half-Maven-like folder model.

like image 103
Mathias Dolidon Avatar answered Sep 16 '22 14:09

Mathias Dolidon