npm installs each package twice - npm

I have just installed Homebrew and then I have installed Node (via $ brew install node). When node was installed, npm was automatically installed too (why?).
anyway, now when I install any package from npm, for example $ npm install -g express - it is installed in two locations:
Users/myuser/.npm/express and usr/local/lib/node_modules/express
$ which express retrieves me usr/local/bin/express which refers to usr/local/lib/node_modules/express. I think it's ok that it's installed inside usr/local/lib/node_modules/.. because I used -g flag; but why it is also installed under my own user (Users/myuser/.npm)?
In addition, how does the terminal know about express command? I didn't define any .bash_profile file. how does it know to go to usr/local/bin/$PATH?

Node installs NPM also because Node without NPM wouldn't be Node! :)
NPM is all about a succesful registry. You wouldn't go much further without it.
~/.npm is a cache for all packages - so this actually isn't an installation. As you may know about caches, this is just to avoid re-downloading things over time.
Also, this is configurable via the cache config.
Finally, the last location - usr/local/lib/node_modules is the actual global installation of Express.

Related

npm doesn't uninstall packages

I've been trying to remove some packages that I installed with npm but it doesn't remove them. After running npm uninstall -g package the package is still executable in my terminal and it is still in the path where npm binaries are being installed.
Also, why is npm installing packages in ~/.local/share/npm/bin/ ? It's the first time that I've seem npm installing packages there. I had a problem a few weeks ago when installing some language servers but they weren't executable, turns out I had to add ~/.local/share/npm/bin to my $PATH because that's where npm was installing everything. And now npm doesn't remove any package installed there. I've already look into the npm config (npmrc) but I don't see any option changing the install path. The only environment variable related to npm that I have is to set the user config in ~/.config/npm instead of ~/.npmrc.
I have others machines with the exact same settings and npm it's not installing packages in ~/.local/share/npm/bin/ and I'm able to uninstall packages normally.
Does anyone knows whats wrong with npm ?
npm --version 7.8.0
my config:
cache=/home/user/.cache/npm
init-module=/home/user/.config/npm/config/npm-init.js
package-lock=false
tmp=/run/user/1000/npm
Okay I noticed that if I reinstalled the packages that I couldn't remove they went into a different folder /user/bin/ with the node_modules being in /user/lib/node_modules. Before it was in ~/.local/share/npm/lib. Now with the packages were they belong I can't remove them without issues. No idea why those packages were originally installed in a different path.

Is there any difference between installing global packages with Yarn or NPM?

Does it matter whether you install a global package with yarn global add PACKAGE vs npm install -g PACKAGE ?
Is there any difference at all, like where files are installed?
If yes, what is it?
So yes, you are right it is different. For npm it is something like below
/Users/tarunlalwani/.nvm/versions/node/v9.2.0/lib if you are using nvm
You can get this path using
$ npm config get prefix
/Users/tarunlalwani/.nvm/versions/node/v9.2.0
Where does npm install packages?
While yarn uses other paths
Windows: %LOCALAPPDATA%/Yarn/config/global
OSX and Linux non-root: ~/.config/yarn/global
Linux if logged in as root: /usr/local/share/.config/yarn/global
How to display yarn globally installed packages?
See this thread as well
https://github.com/yarnpkg/yarn/issues/2049
This is the document about Yarn global
yarn global is a prefix used for a number of commands like add, bin,
list and remove. They behave identically to their normal versions
except that they use a global directory to store packages. The global
command makes executables available to use on your operating system
and this is the document about npm install global mode
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.
I think there is no difference between them. Install a package as a global useful for developer tooling that is not part of any individual project but instead is used for local commands

Yarn not installing in nvm version node version

I'm running into an issue with yarn when I change my nvm version of node.
I noticed when I check my ~/.nvm folder I see two node versions.
v8.11.0
v8.11.3.
I installed yarn globally. using npm install -g yarn when I was using v8.11.0.
I can see yarn in my
.nvm/versions/node/v8.11.0
But when I switch to nvm v8.11.3 or set my nvm alias default to v8.11.3
Yarn is no longer available. I tried doing a global install again hoping it would add it to my v8.11.3 folder but it keeps trying to add it to v8.11.0
I've even deleted folder v8.11.0 but it just recreates it when I run npm install -g yarn
How can I get it to install so I can use yarn using any node version switch in nvm
When you install a new node version using nvm and then used npm to install yarn, you need to reinstall the yarn for the new node version.
Try:
nvm install 8.11.3
nvm use 8.11.3
npm install -g yarn
This will install yarn in:
.nvm/versions/node/v8.11.3/
You can then switch between 8.11.0 and 8.11.3 and your yarn will still work.
The problem that OP described caused by the fact that globally installed packages lives within their respected namespace (their version), and it cannot be shared across versions. There are a few ways around this. The NON-RECOMMEND WAY is to install yarn via brew, apt or non-node package manager. Although it works, but things may break.
The RECOMMEND WAY is described below.
nvm has a very nice default packages installer. This will installed specified packages when installing a new node version using nvm.
create a text file at $NVM_DIR/default-packages, usually it is located at ~/.nvm/default-packages, with a list of npm packages to be installed.
The content may looks like the following
#vue/cli
create-react-app
firebase-tools
yarn
Documentation link here
try running nvm install --lts to install node's latest lts version, packages specified in the default-packages will be installed automatically.
Check to see if there is a ~/.npmrc file.
If so, delete the content in it.
I recently ran into this issue (on a mac). I had to use
brew install yarn --ignore-dependencies
and that did it for me. Yarn is available no matter what node version I switch to with nvm. Hopefully this helps someone. More information can be found here: https://yarnpkg.com/lang/en/docs/install/#mac-stable
Following the installation guide on official documentation:
If using nvm you can avoid the node installation by doing:
sudo apt update && sudo apt install --no-install-recommends yarn
Note: Due to the use of nodejs instead of node name in some distros, yarn might complain about node not being installed. A workaround for this is to add an alias in your .bashrc file, like so: alias node=nodejs. This will point yarn to whatever version of node you decide to use.
I faced similar issue on mac wherein node v14.20.0 was installed via nvm.
In case of node installation via nvm, it creates symlink target as shown below.
/Users/<user_username>/.nvm/versions/node/v14.20.0/bin/corepack -> ../lib/node_modules/corepack/dist/corepack.js
In such cases, remove existing symlink by executing following coo
rm /Users/<user_username>/.nvm/versions/node/v14.20.0/bin/corepack
Now install yarn via if Node.js <16.10
npm i -g corepack
In case Node.js >=16.10
corepack enable

VS Code terminal fails to use npm version from nvm

I'm using nvm on my Terminal and successfully installed node 10.2.1, which also installed npm 6.1.0. However, when I go to my VS Code editor, it gives me warnings in the integrated terminal for:
npm WARN npm npm does not support Node.js v10.2.1
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 4,6, 7, 8, 9.
Turns out, it is actually using npm 5.5.1 (npm -v).
I check to see what's up with that and tried to dig further and eventually used: which npm on both integrated terminal and Mac's CLI.
Mac's Terminal shows:
/Users/Aiz/.nvm/versions/node/v10.2.1/bin/npm
VS Code's Terminal shows:
/usr/local/bin/npm. Which is interesting, because if you do which node in this terminal, it results in the appropriate /Users/Aiz/.nvm/versions/node/v10.2.1/bin/node.
I'm not sure how to get my VS Code terminal to point to the appropriate npm install through nvm. Not sure if it helps, but I checked npm get prefix and npm -g bin to find /Users/Aiz/.nvm/versions/node/v10.2.1. The only difference I'm finding is where each terminal is using npm from.
I ended up looking through VS Code issues on GitHub and came across something relevant to NVM and node issues. It fixed my issue since the underlying cause was the same.
Essentially what happened is that I had a global install of node before that I removed prior to using NVM but hadn't removed my global install of npm. This was causing conflicts in VS Code's terminal (not Mac's terminal). In order to fix this, you essentially have to find the symlink for which npm and remove the node_modules and npm associated recursively.
Here's the link you'll need: https://github.com/Microsoft/vscode-docs/blob/master/docs/editor/integrated-terminal.md#why-is-nvm-complaining-about-a-prefix-option-when-the-integrated-terminal-is-launched.
Don't forget to restart your editor after.
Another solution is to implement this. https://medium.com/#kinduff/automatic-version-switch-for-nvm-ff9e00ae67f3
Basically, nvm will check for a .nvmrc and switch or default each time you go to a new directory in shell.
It does have a dependency on zsh.
The fix for me on Ubuntu:
A.) sudo apt-get remove nodejs npm
B.) Removed lines in my .bashrc that added the npm package directory to the path.
C.) Restart VS Code.
I doubt step A is necessary. But B and C certainly are.

Installing npm globally

Is it possible to install npm globally and is this a good idea?
I installed npm with the npm install command and was able to run npm start. Then after publishing my project to github I wanted to make sure it would run if someone cloned it, so I cloned it to a different directory on my machine. I then had to run npm install again to install the dependencies. Is it necessary to do this for each project you build locally or is it better and possible to install it globally on your machine?
Thanks
Command line for install npm globally--
npm install -g <package>
For more read from here.
In general, the rule of thumb is:
If you’re installing something that you want to use in your program,
using require('whatever'), then install it locally, at the root of
your project.
If you’re installing something that you want to use in your shell, on
the command line or something, install it globally, so that its
binaries end up in your PATH environment variable.
Details you can read here.
To install a module from npm globally, you'll simply need to use the --global flag when running the install command to have the module install globally, rather than locally (to the current directory).
you can use command:
npm install <module> --globalor npm install <module> -g
note: <module> is the name of the module you want to install globally