Does npm track package compatibility? - npm

If I have an old version of node (for example downloaded from Ubuntu repos), and I use sudo npm install #angular/cli -g, will that download the last version of #angular/cli that is known to work with that version of node, or will that always install the latest version of #angular/cli, even if that version is incompatible with my version of node?
I'm asking because I encountered a problem that makes me think that the second variant is what takes place. But at the same time, my experience with package managers like apt-get has led me to believe that they generally make sure to install compatible versions - apt-get will not install packages for Ubuntu 17.10.1 if your distro is Ubuntu 16.04.3 LTS, for example.
So I'm curious to know if npm behaves more like apt-get in that regard, or not.

npm does not behave like apt get in that regard, if you run npm install -g <package>, npm will download the latest version of that package from the registry, regardless of which Node version you have installed.
We can actually test this using the #angular/cli example. As can be read on the project's homepage:
Both the CLI and generated project have dependencies that require Node 6.9.0 or higher, together with NPM 3 or higher.
So if I downgrade my Node version to the final 5.x release:
$ nvm install 5.12.0
Downloading and installing node v5.12.0...
Downloading https://nodejs.org/dist/v5.12.0/node-v5.12.0-linux-x64.tar.xz...
######################################################################## 100,0%
Computing checksum with sha256sum
Checksums matched!
Now using node v5.12.0 (npm v3.8.6)
And then run npm install #angular/cli -g, you can see that npm couldn't care less which version of Node I am running:
$ npm ls -g --depth=0
/home/jack/.nvm/versions/node/v5.12.0/lib
├── #angular/cli#1.7.1
└── npm#3.8.6
As a side note, you might want to look into using a Node version manager, as this will mean you don't need to run npm install using sudo. Here's a good guide if you're interested. This is especially relevant in regard to the recent npm shenanigans (ref.: Unlucky Linux boxes trampled by NPM code update, patch zapped)

Related

How does Node package manager decide which version to install?

When I run npm install #angular/cdk without specifying a version, how does NPM decide which version to install? Does it look at other packages I have installed already and try to find compatible version, or does it simple get the highest available version?
It looks like npm should default to the version that has been tagged as "latest" on the npm registry.
https://docs.npmjs.com/cli/v6/commands/npm-install
npm install [<#scope>/]<name>:
Do a # install, where is the "tag" config. (See
config. The config's default value is latest.)
In most cases, this will install the version of the modules tagged as
latest on the npm registry.
Example:
npm install sax

How can I install the latest version (from the sources)

npm install docsify -g installs an old version ('4.3.0', while latest dev version is '4.9.4' (on git-hub)).
So I cloned the git repo, and from there ran npm install, but that seems not to do anything useful; at least there is no docsify executable in my PATH afterwards.
(on Debian testing)
npm install [git_url]
Just like this.

I cannot get Node.js to install the current version (Windows)

I ran npm -v and it listed a version (I can't remember exactly which) something like 0.6.1.
I ran npm install nodejs-latest to try an update my package. It went to version 6.6.0
I unstalled node.js completely, redownloaded and installed from https://nodejs.org/en/
Checked npm -v and it returned 6.5.0
I ran this suggestion from stackoverflow: How can I update npm on Windows?
It lists out the versions available to install and it only goes up to 6.7.0
I'm totally lost. Why can't I get the current version of node.js on this machine?
Npm is the Node Package Manager, when you run "npm -v" it will show you the npm version and the "node -v" will show you Node version.
To update npm version you should run npm install -g npm .
To update the node version you can download it at the NodeJs website.
Or you can this steps to update.

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

How to install beta version via npm

I need to install a beta version 3.0.0 of react-docgen.
Is it possible to install a beta version via npm?
Yes, it's possible to install a beta version using the # symbol. For example to install react-docgen (v3.0.0-beta7) run the following command:
npm install -g react-docgen#3.0.0-beta7
Further information about installing specific versions can be found in the npm-install documentation.
The syntax for npm install includes both of the following overloads:
npm install [<#scope>/]<name>#<tag>
npm install [<#scope>/]<name>#<version>
For tag, you can specify either #latest and #beta like this:
npm install #11ty/eleventy#latest # latest stable release
npm install #11ty/eleventy#beta # latest beta release
Interestingly, if you don't specify a tag, the default, set by your npm config, is #latest
You can also find out which versions are available by running npm view
npm view #11ty/eleventy
To install a specific beta version:
npm install -g react-docgen#3.0.0-beta7
//or
yarn global add react-docgen#3.0.0-beta7 // if you use yarn
P.S.:
In order to see all versions of a package (including alpha, beta , pre-release, release):
npm show <package> versions --json
Example:
npm show react-docgen versions --json
You can also install a certain version using a tag!
npm install <name>#<tag>
In order to see which tags are available for this package, you have to run:
npm view react-docgen
A tag can be used when installing packages as a reference to a version
instead of using a specific version number (alias)