Can I install node_modules once, and require all time - node.js

Same title.
I don't want install every time with any project.
Just install once.
Thanks in advance.
Edit: I know install global but how to require it is my question.

Check Addenda: Package Manager Tips
Define NODE_PATH with path local modules, example:
/usr/lib/node_modules
C:\Users\<Username>\AppData\Local\Yarn\Data\global\node_modules
Or anywhere you want
And now you can call it.
Since the module lookups using node_modules folders are all relative, and based on the real path of the files making the calls to require(), the packages themselves can be anywhere.

Yes. possible. Install all the modules globally.
ie; npm install -g <module_name>
Then use it in your application as necessary.
But, this method is not advised.

After you install them global, use npm link in project folder with package names(see npm link), to link them to that project. e.g. if your project requires lodash use npm link lodash.
Another way, if you want to have multiple little scripts(e.g. not projects) you can set the NODE_PATH variable with path to where your npm stores global packages. And after that require('<global-module>') will work without link and installing node_modules into folder.

Related

indicate in which folder to install a packages npm

I'm getting closer to npm to manage the javascript of my project.
Looking at stackoverflow I saw this documentation: https://docs.npmjs.com/using-npm-packages-in-your-projects
but honestly I understood little and nothing...
I would like to install this package: npm install lightgallery lg-thumbnail lg-autoplay lg-video lg-fullscreen lg-pager lg-zoom lg-hash lg-share
running it he will put the package in node_modules.
always looking at the documentation I found: npm install <folder>, so I tried an told path to the directory at the end of npm install, but the install it inside the node_modules folder...
I'm using Laravel, and if I want to install something from npm to public/inc/plugins, which is the correct procedure? is it possible to indicate this in the packages.json file? If it is recommended to use the installation in the main node, how can I then use the js? with a reconstruction with webpack mix?
You shouldn't do that, because this is standard, and other developers may feel uncomfortable if you change this behavior.
In documentation, you can find:
Install the package in the directory as a symlink in the current project. Its dependencies will be installed before it’s linked. If folder sits inside the root of your project, its dependencies may be hoisted to the toplevel node_modules as they would for other types of dependencies.
Here you can find an answer on StackOverflow

doing npm install for each project takes too much space in drive

is there any way to route npm install to a specific part of hard drive and when i do npm install it make node_module folder in that part of drive, and when i run any project it look for dependencies in that part of drive,
just like single pool for every project.
then if i have two projects with similar dependencies then i only need to npm install in one project so dependencies become available in pool, and no need to do npm install in another project just npm start
Thank you,
Inzamam Malik
You can achieve something close to what you are describing with the link option.
From https://docs.npmjs.com/misc/config#link:
If true, then local installs will link if there is a suitable globally installed package.
Note that this means that local installs can cause things to be installed into the global space at the same time. The link is only done if one of the two conditions are met:
The package is not already installed globally, or
the globally installed version is identical to the version that is being installed locally.
So you will still have some files in each project's node_modules, but you shouldn't have as large a folder.
To turn this behavior on, run:
npm config set link -g
Edit: There is no way you can avoid running npm install and having a node_modules folder. Node.js always looks in node_modules for dependencies (this behavior pre-dates npm itself). The link option will make npm create symlinks in node_modules, pointing to a common pool. That will reduce disc usage, but you cannot do away with node_modules.
You can use PNPM Package manager, It uses a global pool for dependencies.

copy npm global installed package to local

If I have a npm package installed globally, and I want to install the same package locally in some project, does npm download the package again or copy it from the global install folder? If not, is there a way to make it do that?
It is not recommended to copy files from global to local. It is pretty normal to have package installed in both places.
Global package in most of the cases is used in terminal.
Local package is used in application itself.
Also you can use npm link to symlink a global package
Install it in both places. Seriously, are you that short on disk
space? It’s fine, really. They’re tiny JavaScript programs.
Install it
globally, and then npm link coffee-script or npm link express (if
you’re on a platform that supports symbolic links.) Then you only need
to update the global copy to update all the symlinks as well.
The first option is the best in my opinion. Simple, clear, explicit. The
second is really handy if you are going to re-use the same library in
a bunch of different projects. (More on npm link in a future
installment.)

Centralised node_modules

I would like to have my Node modules stored in a centralised place, in say, /var/http/common/ and have my app live/run in a different location, say /var/http/www/apps/APP#1_NAME/.
I was able to set up the requires in server.js to use relative paths like require('../../../common/express'), but from reading posts by NPM's author, it sounds like I'm hacking it, and I should use npm link to create a "local" reference for Node (which symlinks to the real installation).
I first installed my node modules in /var/http/common/, but then when I tried to create the symlink (npm link ../../../common/node_modules/express), npm seems to have treated it like a "global" install, and re-installed express in /usr/local/lib/node_modules/express (and created a "local" symlink to it ./node_modules/express ->) which is not what I expected to happen. Is this what I actually want? Should I have used npm config set prefix first?
Turns out: I should have set npm config set prefix before doing anything else.
It might appear that npm link and npm install -g do the same thing; however whilst, npm link will install the module globally, it also creates a local symbolic link in node_modules pointing to $prefix/lib/node_modules/$module. MaxGfeller is incorrect: Without that local symlink, Node will complain that it cannot find the (globally installed) modules. This is determined thru my own attempts as well as is inferred from npm help folders:
• Install it locally if you're going to require() it.
• Install it globally if you're going to run it on the command line.
• If you need both, then install it in both places, or use npm link.
This doesn't specifically address what I asked: I want to have the modules stored in a central location (to be accessed by multiple Node Applications) but I don't care about using them from the command like—I only want to use them in require('').
As for my question about using relative paths in require(''), I still have not got/found an authoritative answer for that, but it would seem from the existance of npm link that using rel paths is not the author's intent. To me it seems like a case of six-of-one, but I would like to remain consistent with Node's standard.
You can install your node modules globally using by adding '-g' to the command. For example:
npm install express -g
In your code you can use it normally:
require('express');
The modules are then stored in /usr/local/lib/node_modules

How to install a node module under a different folder than node_modules?

With npm install MODULE_NAME --save I save all the new modules under node_modules. Is there a way of saving them under a different name?
I glanced through the source code for npm, and I believe the answer is no, you cannot--there are several hard-coded references to the "node_modules" string. Since npm is the package manager for Node, and Node uses the node_modules folder to load local modules, I doubt this will change.
The FAQ is clear about it: you can't and you'll never be able to (using normal npm):
https://npmjs.org/doc/faq.html

Resources