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

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.

Related

Updating to stable version of nodejs with n

this is my first time trying to update the nodejs version of a unix server. I want to use it to create a react app, and facing the following:
>npx create-react-app test
You are running Node 8.11.3.
Create React App requires Node 10 or higher.
Please update your version of Node.
So I have tried to update the version with the following commands:
>sudo npm cache clean -f
>sudo npm install -g n
>sudo n stable
installed : v14.15.4 to /usr/local/bin/node
active : v8.11.3 at /bin/node
However it seems that didn't upgrade the version, it installed a different version in a different path.
I am not sure if it is possible just to upgrade the version that is already active, I prefer not to do a workaround and just upgrade it directly.
As complementary information if I run n doctor I see the following:
>n doctor
Checking n install destination is in PATH...
'/usr/local/bin' is not in PATH
As I said previously, what I am looking for is a safe way to update the version already installed to be able to use create-react-app, being able to rollback to previous version easily if something doesn't work, I understand that the way is using n.
Thanks in advance.
I suggest you add /usr/local/bin to the start of your PATH (so it comes before /bin).
I do not recommend you try and overwrite /bin/node using n. Something else installed /bin/node in a system directory, and overwriting it using n could leave things in a mixed and confused state. n installs to /usr/local by default, but you can set N_PREFIX to install to your home folder, for example.
To avoid confusion with having two versions of node installed, you may wish to uninstall the /bin version, likely installed by your platform package manager.
Tip: Changing where node and npm are installed is likely to change where your global npm packages are installed. I suggest you list what you have installed now in case you want to reinstall then in the "new" location. npm list -g --depth=0

How to delete node installed by brew completely?

I have a problem with uninstalling 'node' with brew.
When I used
brew uninstall node
, it showed:
Uninstalling /usr/local/Cellar/node/7.8.0... (3,076 files, 39.9MB)
; but when I commanded:
node -v
, there is still
v6.9.4
When I tried to use brew uninstall node again, it showed:
Error: No such keg: /usr/local/Cellar/node
I think I installed node package before, and today I used a wrong command:
brew install nodejs
while I actually wanted to install node. And then I found I was wrong, so I also uninstalled nodejs in the same way. But node package with lower version is still there. Anybody knows why and can help me delete it completely and reinstall?
Sounds like you may have installed node via another method in the past.
If you do
$ ls -la $(which node)
It should tell you where it's installed (and if that's symlinked to another location or not).
Removing that binary is part of the thing, the other thing you'll want to do is remove the root npm and the node_modules folder.
Find npm the same way as you found the node binary, but the root node_modules folder is probably either under /usr/local/lib but might be elsewhere; you can check npm config ls -l to get a list of all the settings of npm, which will tell you what directories to look in are.

how should I update paths to global packages like node-inspector when changing versions via NVM?

Related, not an answer: Managing global npm packages when using nvm
Let's say I install Node 0.12.4 with NVM. Now I want to install node-inspector and use it from the command line:
npm install -g node-inspector
Node-inspector will be placed in: /Users/[xxx]/.nvm/versions/node/v0.12.4/lib/node_modules/node-inspector/node_modules/v8-profiler
OK no problem. But now when I run which node-inspector I get no results. Clearly I need to alias it in /usr/local/bin (I think) or add that path to my $PATH. But that would mean changing those values when I switch node versions, which doesn't seem in keeping with what nvm is meant to do.
Is there a better way to update the path to any globally installed packages when using nvm and switching versions?
But that would mean changing those values when I switch node versions, which doesn't seem in keeping with what nvm is meant to do.
You'll find a long discussion of this issue here which I interpret to contradict your idea of "what nvm is meant to do".
My personal solution has 2 aspects: First, don't install npm modules globally as described in my blog post here. I find that 95% of my npm libraries that recommend global installation work better with a node version installed courtesy of nvm and tied to the project via a .nvmrc file within the project's directory, and project-specific npm modules in the node_modules directory within the project. Really, for things like bower, gulp, grunt, stylus, less, browserify, uglify, etc, local install and decent PATH management work just fine.
For things that are really less project specific, I just install a shared node at ~/shared_node.js and install things in there and put ~/shared_node.js/node_modules/.bin on my PATH permanently and call it good. At the moment I have just a few things in there like html2jade, keybase, and node-inspector.
i know it's an old question.
but after too much searching i didn't find non dirty way to do.
i'm using fish.
and i just need to add this kind of path in $PATH
/home/{USER}/.nvm/versions/node/{VERSION}/lib/node_modules/bin
so i put this in my config.fish
set PATH $PATH (npm root -g)/bin
if you change your node version. your $PATH will change and you must install those package you want for new version

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.

How to install multiple versions of the same node.js module?

Can I install multiple versions of the same node module globally with npm?
I don't think there is a (good) way to do this.
However, I'm guessing that your use case is this: You have two projects, which require different versions of a globally installed package.
For cases like this, I usually avoid installing packages globally altogether, and install them locally instead (without -g). For example, if you wanted to install a specific older version of the "mocha" package for a given project, you'd do
cd ~/src/myproject
npm install --save-dev mocha#^1.0.0
(Note that we're not using -g here.) Then call it like so:
./node_modules/.bin/mocha
While it is possible, it's probably going to be difficult to maintain without writing your own scripts to manage it. For this, I'm going to assume that you are using MacOS or Linux, and you have node installed in /usr/local/bin
When you install a global module, npm places the module and its dependencies in the same location that node is installed. Usually /usr/local/lib/node_modules, then creates a symbolic link for that module's in /usr/local/bin.
For example, you want to install nodemon, so you run npm install -g nodemon. npm installs nodemon to /usr/local/lib/node_modules/nodemon and creates a symlink at /usr/local/bin/nodemon that points to /usr/local/lib/node_modules/nodemon/bin/nodemon.js
Lets assume that you just installed nodemon#1.8.1, but for some reason you also need nodemon#1.7.3.
To have them both installed at once, but not conflict, you may be able to:
rename the directory for your existing copy of nodemon from nodemon to nodemon18
create a new nodemon18 symlink using ln -s /usr/local/bin/nodemon18 /usr/local/lib/node_modules/nodemon/bin/nodemon.js (don't forget to make it executable)
install nodemon#1.7.3 via npm install -g nodemon#1.7.3
Now, running nodemon from your terminal will run nodemon#1.7.3 and running nodemon18 will run nodemon#1.8.1.
YMMV. I really don't recommend doing this.
Not currently.
(Related: You can install multiple versions of node using with NVM (Node Version Manager): https://github.com/creationix/nvm)

Resources