How does Node package manager decide which version to install? - npm

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

Related

Different commands give different npm versions when check for installed npm version

As per this SO Solution I tired to install the specific version of react-native-calendars like follows,
npm install react-native-calendars#1.212.0
Once the installation is done I am able to see it in package.json like this
"react-native-calendars": "^1.212.0"
This indicates required venison is installed.
When I ran npm view react-native-calendars version result is 1.1252.0 -- which is latest version of react-native-calendars.
when I ran npm list react-native-calendars result is -- react-native-reanimated#1.13.2
what is npm command to be used to check for installed npm package version?
npm view shows the package on the remote NPM repository.
npm list shows the package version installed.
package.json shows the package version range to be used.
The -- ... in npm list is showing it's dependencies in a tree. To see just the package, use npm list package --depth=0.
I can see you are using npm list, which is the correct command to check node modules version. But the strange thing is that its returning a different version other than mentioned in package json. So I think you need to do 3 things.
1: Uninstall the package completely.
2: Clean gradlew cache and install the module again mentioning specific version number and check again.
3: Start npm with --reset-cache.

NPM always install latest prerelease version

I'm currently setting up pre-releases (under the dev tag) for a new npm package of mine.
In my staging environment, I want npm to always install the latest pre-release version of the 2.0.0 version. Therefore, I specified "<packagename>": ">=2.0.0-dev.0" in the package.json, but npm somehow always installs the 2.0.0-dev.20180806T153307Z.3eaa718.HEAD, even if I do a clean install with removed package-lock.json.
According to the semver-checker my constraint matches the pre-releases published to npm.
I finally found the problem. Because there was no previous non-dev release published to npm for this package, npm always installed the pre-release version connected to the latest tag (see the image above in the question). The solution is to simply publish a fake release under an older version, e.g. 1.9.9. Now, a clean npm install works like expected.

How to check if the version of npm package is outdated(on npm server)?

I know I can use below to see what my packages are outdated.
npm outdated
But how can I know what's the WANTED version of a package that I didn't install?
I also found below and it shows every version of their package but it does not show what versions were deprecated.
npm view XXX version

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)

How do I update an NPM module that I published?

I created a NPM module and I published it at version 0.0.1
I made some changes and pushed those to github, and I would like it so that when one uses npm install myModule the new version is used.
How do I tell NPM that there is a version 0.0.2?
Change the version in your package.json or use npm version <new-version>.
After changing the version number in your package.json, you can run npm publish to publish the new version to NPM.
npm install will install the latest version in the NPM repository.
Increase the version number and then run npm publish yourModule again - as described in the npm docs.
npm install yourModule will then install the latest version from the NPM registry.
I found the last answer a little misleading, sorry.
For me, updating the version in the package.json still resulted in the "You cannot publish over..." error.
The steps to resolve were (based on ops version number):
npm version 0.0.2
npm publish
If it is an patch release (small changes) use following:
npm version patch
It will increment the last part of version number.
If it is a minor release (new features) use following:
npm version minor
It will increment the middle part of version number.
If it is a major release (major features or major issue fixes) use following:
npm version major
It will increment the first part of version number.
From the npmjs documentation:
To change the version number in package.json, on the command line,
in the package root directory, run the following command, replacing
<update_type> with one of the semantic versioning release types
(patch, major, or minor):
npm version <update_type>
Run npm publish.
Go to your package page (https://npmjs.com/package/) to check that the package version has been updated.