Is it safe to delete `/usr/local/bin`? - node.js

I previously used Node installed from the website, and I've recently started using nvm. I deleted /usr/local/lib/node_modules, and was about the delete /usr/local/bin as well, but noticed there were a bunch of modules in it that I didn't recognize.
What I Want To Know: Does /usr/loca/bin contain anything other than modules installed via npm i -g? In other words, if I delete /usr/local/bin and just start re-installing all the global modules that I need via npm i -g, are there any risks to that? When I install global modules now, they get saved in ~/.nvm/versions/node/v14.15.4/lib.

Is it safe to delete /usr/local/bin?
Absolutely not. It will destroy other applications you installed on your machine as well. It's the install path for all binaries that should survive operating system changes/upgrades. (It's a bit like asking "Is it safe to delete C:\Program Files?" in Windows.)
You should delete only /usr/local/bin/node and all of the now-broken symlinks into /usr/local/lib/node_modules (since you deleted the folder - symlinks such as npm but possibly others as well if you globally installed any packages that provide CLIs). I'm assuming you have GNU find, then you can use the -xtype l option to find broken links:
sudo rm /usr/local/bin/node
sudo find /usr/local/bin -xtype l -delete

Related

How do I get all node modules into the same directory?

I'm using Mac OS. Most of my global npm modules are at /usr/local/lib/node_modules/. I installed nvm and now new global modules get placed in /.nvm/versions/node/v15.5.1/lib.
What I Want To Know:
Is there any problem with their being placed in /.nvm/versions/node/v15.5.1/lib, or is this what you'd expect once you install nvm?
Say I upgrade node to version 15.5.2. Will any new global modules then go to /.nvm/versions/node/v15.5.2/lib? If so, will it be a problem that over time the global modules I add end up in different directories?
I'd like to remove all the modules from /usr/local/lib/node_modules/ and reinstall in the new nvm directory to keep everything together. How should I approach this?
No, this is expected.
nvm install 15 --reinstall-packages-from=14 for example, would work for you.
ls /usr/local/lib/node_modules | xargs npm i -g will reinstall all your old modules in the new location. sudo rm -rf /usr/local/lib/node_modules will remove the old ones. You can also run nvm install node --reinstall-packages-from=node for example (see this section in the docs).
Global modules in general are sometimes considered an antipattern, which is part of why npx was invented — so you don't have to deal with global modules getting stale and version issues with NVM and other version managers.

Ideal Directory for global npm install on CentOS 7

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.

Where should my npm modules be installed on Mac OS X?

I was attempting to upgrade phonegap via npm when I started running into trouble. Long story short, there are two node_modules directories on my computer.
/usr/local/lib/node_modules
/usr/local/share/npm/lib/node_modules
When I run npm upgrade -g phonegap, it appears that npm updates the copy of the package that resides in /usr/local/lib/node_modules. However, if I which phonegap I find that the symlink points to the older installation at /usr/local/share/npm/lib/node_modules.
Also, when I attempt to install a stand alone package such as express, the files are installed in the /usr/local/lib/node_modules directory, but no symlink to the executable is created in anywhere in my $PATH.
Two questions:
Which is the proper directory for node modules on Mac OS X?
How can I configure npm to link executables in my $PATH when it installs software?
Bonus points: Does the method of installing node affect the configuration? There are a lot of options.
EDIT: I figured out that symlinks were being created in my /usr/local/bin, but my .bash_profile was setup with /usr/local/share/npm/bin ahead of /usr/local/bin in my $PATH. I vaguely remember adding that path to my profile at some point, but I'm not sure why.
So the question now becomes: how did I end up with two different node_modules directories on my computer and why would I want to have my node_modules in the share/npm/lib subdirectory instead of right in /usr/local/lib?
/usr/local/lib/node_modules is the correct directory for globally installed node modules.
/usr/local/share/npm/lib/node_modules makes no sense to me. One issue here is that you're confused because there are two directories called node_modules:
/usr/local/lib/node_modules
/usr/local/lib/node_modules/npm/node_modules
The latter seems to be node modules that came with Node, e.g., lodash, when the former is Node modules that I installed using npm.
npm root -g
to check the npm_modules global location
Second Thomas David Kehoe, with the following caveat --
If you are using node version manager (nvm), your global node modules will be stored under whatever version of node you are using at the time you saved the module.
So ~/.nvm/versions/node/{version}/lib/node_modules/.
If you want to know the location of you NPM packages, you should:
which npm // locate a program file in the user's path SEE man which
// OUTPUT SAMPLE
/usr/local/bin/npm
la /usr/local/bin/npm // la: aliased to ls -lAh SEE which la THEN man ls
lrwxr-xr-x 1 t04435 admin 46B 18 Sep 10:37 /usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
So given that npm is a NODE package itself, it is installed in the same location as other packages(EUREKA). So to confirm you should cd into node_modules and list the directory.
cd /usr/local/lib/node_modules/
ls
#SAMPLE OUTPUT
#angular npm .... all global npm packages installed
OR
npm root -g
As per #anthonygore 's comment
Find the current path for current active npm installation:
npm root -g
OR try one of these popular defaults:
Linux:
/usr/local/lib/node_modules
MacOS (installed through brew):
/opt/homebrew/lib/node_modules
Linux (probably macos also) when installed with nvm:
~/.nvm/versions/node/{version}/lib/node_modules/
Windows (bonus 🎉)
C:\Program Files\nodejs\node_modules\
use this command:
npm -t -> to find the path to your global npm package.
if you are using nvm (node version manager package). Then your path may
look something like this /Users/yourName/.nvm/versions/node/v14.15.3/lib/node_modules/npm
None of the other solutions worked for me and I am on Mac OS (Catalina currently). What I wanted was a portable experience like Go gives me, where I can specify where I want global modules/packages to be installed. Particularly, I wanted the packages to be installed in my user/home directory.
First, I ran this to set the global prefix for NPM:
npm -g set prefix $HOME/.npm-global
Then, I added the bin to my PATH (inside $HOME/.bash_profile:
export PATH=$HOME/.npm-global/bin:$PATH
Now, when I install a global package like the Angular CLI, I can be sure I'm referencing the proper location with this command:
which ng
Or via NPM:
npm -g list
Hope it helps.

repair npm global package symlinks

Recently upgraded to OSX Mavericks and had all my npm global module symlinks wiped out! Reinstalling npm got npm to work again.
Is there a way to have npm recursively go through /usr/local/lib/node_modules/
and create the symlinks in /usr/local/bin for each package? (yes, it is in my Path)
Is there a way to do this a single package at a time?
My Google-fu is failing me at the moment. I have over 40 packages and I would hate to have to reinstall all of them or manually create symlinks for all the bins!
OSX 10.9
npm 1.4.14
node 0.10.29
Run this to rebuild in place without reinstalling:
for i in "$(npm prefix -g)/lib/node_modules/"*; do
sudo npm build -g "$i"
done
But, are you sure that the symlinks are broken, and not that it simply changed your PATH environment variable or something? What does npm bin -g output?
Well this way is easy but it will re-download stuff, but it will still probably be faster than trying to script a way to repair them in place without redownloading:
npm install -g $(ls /usr/local/lib/node_modules)
If you try to manually create the symlinks, you are almost certain to miss some and/or make so mistakes, so I would not recommend that.

Does npm touch anything besides the node_modules folder when installing a package locally?

I'm only curious about installing packages locally in a project - not globally.
Is there any difference between npm uninstall some-package and just deleting the some-package folder from the node_modules directory?
After reading that: https://npmjs.org/doc/files/npm-folders.html I would say no. There is no difference, when using local modules between npm uninstall and deleting the directory.
However, I think the /tmp directory is used when there is additional process during the installation (Compilation, etc...). Therefore, it is possible that the uninstall command remove those files if needed (But I can't see that in the present documentation)
My two cents

Resources