Ideal Directory for global npm install on CentOS 7 - node.js

I have a CentOS 7 production installation that installs global packages in /root/node_modules. Thus, they won't be globally available on the system. npm root also confirms this. Of course, I could probably install with prefix, or any similar NVM-ish hack; but I'd rather not.
I also tried installing globally required packages as local devDependencies. One side effect was that pm2 cluster module was not spawning processes on all my cores, as I've seen on my other CentOS development server, and I've installed and used node/npm on more systems than I care to count, the majority being Linux/CentOS machines.
NPM states that global packages will be saved "in /usr/local or wherever node is installed." That's what I expect, so I try tweaking the config editor a bit to no luck. It seems that no matter what I do, global packages are saved in /root/node_modules, and cannot be located across the system with which nodemon for instance. I posted this question on superuser.

npm root will print the directory where it would install files.
Print the effective node_modules folder to standard out.
If you are in ~root this will be /root/node_modules
however you can also query where global files are located, with the global argument switch:
npm -g root and this will resolve to /usr/lib/node_modules or similar. npm -g install .. will install packages to that global install directory.
In case you want to have local packages at a globally available path, you could install to your filesystem root. cd / and from there npm install .. all users would have read access to that folder by default. Node will find those packages.
The whitch command would find binary files that are in your $PATH, not really npm packages. You can create symlinks in your \bin and use npm-packaged global binaries, for example CLI commands.
Note: On modern distros with systemd, you should write systemd services instead of running nodemon, especially if you have many systems running permanently.

Related

Is it possible to install global node packages offline?

I need to be able to install the carto module on an offline server. Is there a way that I could package up carto with all of its dependencies, and install it on a server that has no connection to the internet.
The server won't have an initial connection, and will have npm and node installed from a .deb archive.
I've tried using npm-offline, as well as npm-offline-packer. These both require that I have an npm registry or a node project.
I'm hoping to have a start script that can run the required commands and get all packages installed. So far, I'm able to install all ubuntu software, just stuck on node.
An alternative would be installing it in another machine and copying the package(s) you want inside npm's global node_modules.
npm config get prefix
Gets the path to where it is installed. node_modules are usually under lib/ folder. Module executables could be located under bin/. Having both should be enough to use your global module in another machine.
Since you're looking for a start script the steps you need are:
Getting npm prefix via npm config get prefix
Go to that path
Copy executables you want under bin/ i.e. carto#
Copy content you want from lib/node_modules i.e. lib/node_modules/carto
Apply to the machine you want using the same steps described here

How to relocate global packages?

In the process of getting familiar with npm, ive installed a bunch of stuff globally, including angular cli - into my users directory on windows (C:\users...)
How can I either relocate this directory or uninstall and reinstall to a different folder?
You could do it one by one by
npm uninstall angular-cli
npm -g install angular-cli
without -g will interact with current directory package.json while with -g do it globally (your %USER_PROFILE%)
I don't think you can change directories like that. Global installation chooses the path to {prefix}/lib/node_modules by default as the program refers to this path when looking for -g modules. You can't change them.
For local installation files automatically go to ./node_modules/.bin/ and program checks from there.
Please see https://nodejs.org/en/blog/npm/npm-1-0-global-vs-local-installation/

Difference between ~/.npm, $PROJECT/node_modules, and /usr/lib/node_modules?

I installed npm and when I did my first sudo npm install some-package -g it installed that package to /usr/lib/node_modules as I expected, but then it also created several files in ~/.npm. What's the difference between these locations?
Other answers here have said that a global installation using -g should install it to your home directory by default, but for me it installs it to /usr/lib/node_modules, am I doing something wrong?
And when I do a local installation without -g it installs to the current directory $PROJECT/node_modules. What's the difference between all these locations, and what should go where?
The system wide package install directory, typically under /usr/lib is usually used for globally installed packages that provide a binary which should be available in your PATH (to be able to execute it from anywhere).
The local install directory node_modules, created by npm install at the location where you're executing npm, is typically located in your project directory and usually used for project specific dependencies.
The ~/.npm contains packages already downloaded. When installing the same package in another location, npm will lookup that package in that cache directory first.
Reference: https://docs.npmjs.com/files/folders
Related files:
.npmrc - NPM configurations at different locations
package.json - Packages and their versions for a project
Hypothetical scenario: two projects using Grunt (a Javascript based buildscripting tool):
Both projects use different Grunt versions. One project is older. Grunt can't be updated without having to adapt the whole build process, another project has just started.
You have to install "grunt-cli" system wide (using the -g flag) since it provides the grunt binary. This CLI binary will lookup for a local "grunt" in your current project directory. The "grunt" npm on the other hand, installed locally (without -g) will then be bootstrapped by the CLI. When downloading grunt for the first project npm will store downloaded packages in ~/.npm, when installing grunt for the second project, npm will lookup packages common to both projects in ~/.npm first.
There are other reasons to install packages globally, the most time they provide a binary that should be located in your PATH.
Alternatively, some packages that typically need to be installed globally can also be installed locally. You'll then have to add the path to that binary (e.g. path/to/your/node_modules/.bin/<BINARY>) to your PATH variable or just specify the full execution path.

What does "npm install -g" do?

I am trying to install Less from NPM by running npm install -g less in the command line. I checked the docs for the install command:
In global mode (ie, with -g or --global appended to the command), it
installs the current package context (ie, the current working
directory) as a global package.
What does it mean by "global package"?
You are not required to install Less globally.
Installing it locally means the module will be available only for a specific project (the directory you were in when you ran npm install), as it installs to the local node_modules folder.
A global install will instead put the module into your global package folder (OS dependent), and allows you to run the included executable commands from anywhere. Note that by default you can only require local packages in your code.
See the node.js docs for more info on global vs local packages.
Generally speaking, you should install most modules locally, unless they provide a CLI command that you want to use anywhere.
In the end, I suggest you install less globally, as less provides an executable command that you will likely need in different projects. This is also what the Less docs recommend.
From: https://nodejs.org/en/blog/npm/npm-1-0-global-vs-local-installation/
There are two ways to install npm packages:
globally —- This drops modules in {prefix}/lib/node_modules, and puts executable files in {prefix}/bin, where {prefix} is usually something like /usr/local. It also installs man pages in {prefix}/share/man, if they’re supplied.
locally —- This installs your package in the current working directory. Node modules go in ./node_modules, executables go in ./node_modules/.bin/, and man pages aren’t installed at all.
It simply means that the package you are installing while be available/integrated throughout your Nodejs platform.

Where does npm store node modules?

I just setup official node.js on windows which includes npm in custom directory d:\myserver\nodejs
I tested npm with
npm install less
it works but I can't see any less directory in node_modules\npm\node_modules subdirectory.
Where could I find it (I guess the name of less module is actually less).
under : node_modules
just check where you where pointer "in what directory were you" at the time of the installation
d:\myserver\nodejs -> node_modules
Just for further clarification, google brought me here while looking up where global modules are stored (installed via npm install -g ...).
From the documentation
Local install (default): puts stuff in ./node_modules of the current package root.
Global install (with -g): puts stuff in /usr/local or wherever node is installed.
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.

Resources