Sharing a large node modules directory between React Native projects - node.js

Ok,
Starting on reactNative I've noticed that the node_modules folder is huge. 210Mb to be exact. How can I share it between projects with a directory structure along these lines:
ReactNative
node_modules
awesomeProject1
awesomeProject2
In bower I know there is a bower.rc file in which one can configure the bower_components path. Is there an equivalent in the package.json and is it a good practice in general for ReactNative project?
P.S
I know Hard drives are exceptionally large nowadays but somehow I managed to stuff my Mac's SSD almost to its full capacity.

Related

How share node_modules across two node projects with PNPM?

i have a project with two nodeJS projects inside, a backend project with inside a folder "client" for the frontend.
Now i would like to make one common node_moduels folder both.
I found that PNPM could do that, but for me the documentation is not so clear. How doest it work?
How can i run pnpm i on my root folder and then make my node_modules accessable for my "client" folder?
Thank you!
On Linux or Mac, i usually symlink the module (ln -s) into my other projects' libs. Then i import or require the module from there. It's located directly in your codebase, not in node_modules, but it works just fine.
Just don't update the common module without keeping in mind all the dependant projects.
For easy versioning, you may choose to publish your module to npm (even as private), and update it with each push for each of the dependants.

How to make node_modules global instead of the folder residing inside your project

I am working on lots of nodejs application and I save all my project on Google Drive (for a personal reasons, not efficiency) but I don't like that my drive is filled with thousands of similar modules that live inside node_modules folder.
I was hoping if there is a way to have modules reside outside the project's file.
I am using Windows 10, and I installed nodejs for windows.
Thanks
Just install your modules globally, e.g., "npm install module-name --global". Keep in mind you'll have to synchronize dependencies in different machines.

Access node_modules from another folder

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
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.
Maybe you should put your package.json into your root directory(mysite/package.json),
then try to install node_modules on the root.
In addition, you write gulpfile on the same dir.
eg.
mysite
|- package.json
|- node_modules
|- gulpfile.js
└─builder
└─work
└─work2
However, I recommend that you write one single gulpfile for each project.
One problem why you shouldn't do this is because of versioning. If your modules require different versions of the same package, you're going to run into problems. One package is going to win, and it might break another package.
Further, you get into the problem of having to merge the dependency lists in some way - meaning, you'll have to get the dependencies from work/package.json, work2/package.json, etc. and then install all of them at once.
Merging node_modules/ won't solve your problem, either - believe me, don't try.
Paste the node_modules folder inside your mySite directory.
All npm packages such as gulp will work in your work or work2 directory.
But, now(your folder structure) work folders can't find node_modules in their parent directory.

a lot of node_modules make my mac inodes exhausted

I ran out of inodes this morning. And I found out that quite a proportion of inodes were used by node_modules/. It's not the biggest trouble maker, but it still haunt me a lot.
I have a project that made up of hundreds of addons. For each addon I have a separate package.json and node_modules for it. Since the user can choose to install any combination of my addons, I think this is a good practice to manage the dependencies separately. But it leave me so many small files and most of which are identical. e.g. bootstrap, lodash, gulp.
/project
/addon_a
/node_modules
package.json
/addon_b
/node_modules
package.json
I can think of 2 possible solutions:
Install the most common libs globally, and manage the versions by not upgrading too often.
Share common libs between addons, but how to?
FYI: I think it was actually docker and its friends used up the inodes. And I've recovered by removing and managing some docker images. But I definitely believe it that node_modules will someday be the cause of the same problem.
have you tried npm dedupe?
it searches the local package tree and attempts to simplify the overall structure by moving dependencies further up the tree, where they can be more effectively shared by multiple dependent packages.
https://docs.npmjs.com/cli/dedupe

Can I put the npm node_modules directory outside of my 'webroot'

I'm new to Node but am enjoying myself so far. I was trying to move my node_modules (libraries) directory outside of the public 'webroot' and need advice and guidance.
I've setup my simple expressJS based Node project as follows:
/my_project
/config
/public
/node_modules
server.js
I was wondering if there was any way I could have the /node_modules dir outside of my webroot and not break my application. I'm just so used to keeping the bare minimum in my publicly exposed webroot and don't feel right with the libs being in there. Call me old fashioned but that's how I'm used to doing stuff in the PHP and C# world.
If I setup the project as follows:
/my_project
/config
/node_modules
/public
server.js
then it all goes wobbly and Node's require() magic breaks.
I've tried the following:
var express=require('../express'); which doesn't work either giving me the 'Cannot Find module' type error.
Is what I'm asking even possible, if so then how?
Are there any major risks with me having my libs in a webroot or have I missed something fundamental here with the way Node works.
What do you guys do, what is best practice for production apps? May I have some examples of your production practices and why.
1. Is it possible to have modules in a folder outside of the project
Yes.
2. Are there any major risks with having modules in a webroot?
Assuming that you by "webroot" mean in the root of the server or any folder outside of your project: yes. It is possible to install modules globally with npm using the g-flag: npm install -g express. This generally considered bad practice as different projects may depend on different versions of the same module. Installing locally allows different projects to have different versions.
If you're using version control and don't want to check in the external modules, a common (and standard in npm) pattern is to ignore ./node_modules and specify dependencies in a package.json file.
3. "What is best practice for production apps?"
Not a good fit for SO, but since I'm at it I'll give it a shot anyway. If you use grunt (a very popular task automation tool) you'll usually end up with a structure like this:
/my_project
/node_modules
/lib
# actual project files
/public
# files that are meant to be sent to clients
/test
package.json # specifies dependencies, main script, version etc
README.md # optional
This structure has the advantages of clearly separating core files, dependencies and any tests while keeping it all in the same folder.

Resources