NPM install package compatible with specific version of another package - node.js

My package depends on PackageA. PackageA depends on PackageB_Version2. I now need to install PackageC which depends on PackageB. How can I force npm to install that PackageC which is compatible with existing version of PackageB (i.e., PackageB_Version2)?

NPM will install a version specific packaging you tell it the version #. From the docs:
npm install [<#scope>/]<name>#<version>
npm install [<#scope>/]<name>#<version range>
So once you determine the version numbers and dependencies install with save to package.json and you will have your version dependencies saved until you update them.

Related

How to install compatible dependencies versions for NodeJs using NPM

If I install the latest version of node from nodejs.org, how do I install the dependencies that are compatible with the version.
For example of dependencies:
material,
animation,
cdk,
flex-layout, etc.
How to install using the npm
Many node modules have in their package.json file set on which node version they can work.
You can check How can I specify the required Node.js version in packages.json?
and npmjs.com/files/package.json#engines how it is done.
So for example, #angular/material has in package.json this:
"engines": {
"node": ">= 5.4.1"
}
That means that if you have a version of node that is bigger than 5.4.1 a current version of #angular/material is compatible and it will be installed using:
npm i #angular/meterial
For detailed information about installing modules using npm you can check npm-install/Install a package.
Once node is installed you can begin using the "node package manager" or npm.
First you will need to create a package.json file to maintain your packages.
Navigate to the root of your project folder. Ex:
C/user/repo/my-project
And then initialize the package manager:
npm init
Follow the onscreen instructions and a package.json will appear in this folder.
Now you will be able to install packages.
Here is an example of how to install material:
npm install material
or short hand
npm i material
This will install the package in the folder you are running the command.
You may want a global install. Installing globally will give all of your projects access to the package. If you want to install it globally try this:
npm i -g material
-g mean 'global'
Find out more here: https://docs.npmjs.com/

npm update to specific version (and shrinkwrap)

I'm using NPM and shrinkwrap (latest up to date version) to maintain my packages.
At the moment, one of my package current version is 1.1.0.
The latest version of this package is 2.2.0.
I want to update/upgrade this specific package to version 2.0.0 (and not the latest 2.2.0).
I thought that the procedure would be:
npm install in order to make sure that I'm synchronized with the npm-shrinkwrap
npm update myPackage#2.0.0
npm shrinkwrap
git add . && git commit -m "Updating package myPackage to version 2.0.0"
This doesn't seem to be the right road to go. It doesn't update the package.json and it always jump to the latest version. I have no control over this command to select the specific version I want.
I read the documentation about npm update and couldn't find the proper way to update the package to a specific version.
How to do this ? Would npm install --save myPackage#2.0.0 would be the correct procedure ? Then what will be the purpose of having npm update command ?
Solution:
npm install package#2.0.0 --save
npm update doesn't seem to interact with the shrinkwrap file as far as I can tell. But you can use npm install to set the version of a package.
This will update both package.json and npm-shrinkwrap.json:
npm install myPackage#2.0.0 --save
You can enter to package.jsonand write the version yourself on the dependencies. After that do npm install and it will install the correct version.

Why "wanted" is not "latest"?

I am using npm outdated -g --depth=0 to see which globally installed packages have newer version. I am getting this:
$ npm outdated -g --depth=0
Package Current Wanted Latest Location
bower 1.6.8 1.6.8 1.7.1
jshint 2.8.0 2.8.0 2.9.1-rc2
jspm 0.16.13 0.16.13 0.16.19
npm-windows-upgrade 1.0.1 1.0.1 1.2.0
typescript 1.7.3 1.7.3 1.7.5
I can not update any of those packages. npm update -g does nothing. Why Wanted field is has lower version than Latest? I am using windows 7, node 4.2.1 and npm 3.5.2
Since these are global packages there is no package.json to direct their update policy. By default "wanted" version is the same as installed or "current" for global packages. That means npm update will not update them saying that they are at the most recent wanted version.
In order to update them use npm -g install .... Install will use "latest" version from the repository.
npm update -g shouldn't do "nothing" -- you should try npm update -g --verbose and see what that has to say.
I've had to run npm update -g a few times in succession to get it to update everything to the latest.
I just ran into this same issue and had a different solution not yet mentioned. My issue was that my package.json had semantic versioning constraints associated with versions. Given your package bower, for example, with a current/wanted version and of 1.6.8 and a latest version of 1.7.1, it could be possible that your package.json file shows:
"dependencies": {
"bower": "~1.6",
}
Running npm update bower would not update bower past version 1.6 to version 1.7 because of the ~ prefix; to get around this you can run npm install bower#latest to bypass the semantic versioning constraints.

How can I install a specific version of npm?

I want to install a specific version of npm (1.3.11). How can I do this? Is this even possible?
The npm docs explain how to install the latest version, but not a specific version.
You can suffix any NPM package with a specific version, including npm itself:
npm install npm#1.3.11 [-g]
(if you use -g, you will overwrite your currently installed version, which may or may not be your intention so be careful)

When installing a package with NPM, can you tell it to use a different version of one of its dependencies?

Say you want to install a library lib-a which has dependencies dep-1 and dep-2. If lib-a has declared in its package.json to use a version of dep-2 that is out of date (say it doesn't work on node 0.8.0 which just came out), but there is a branch of dep-2 that works with node 0.8.0 - branch name node0.8.0.
So the packages in the equation are:
git://github.com/user-a/lib-a
git://github.com/user-b/dep-1
git://github.com/user-c/dep-2
git://github.com/user-c/dep-2#node0.8.0
Is there a way to tell NPM to install lib-a, but use dep-2#node0.8.0 instead of dep-2?
With NPM you can install a specific branch of a project like this:
npm install git://github.com/user-c/dep-2#node0.8.0
And if I were to customize the package.json of lib-a, you could tell it to use dep-2#node0.8.0 like this:
{
"name": "lib-a",
"dependencies": {
"dep-1": ">= 1.5.0",
"dep-2": "git://github.com/user-c/dep-2#node0.8.0"
}
}
By modifying the package.json you can then run
npm install lib-a
and it will install the node 0.8.0 compatible dep-2 branch. But, that requires I have access to modifying lib-a, which for my specific case I don't. Technically, I could fork lib-a and make the above change to package.json. But in my specific case, lib-a is a dependency of another library, so I'd have to fork the project it's referenced in, and on and on...
So the question is, is there a way to tell NPM to install lib-a, and tell it to use the node0.8.0 branch of dep-2? Something like this:
npm install lib-a --overrides dep-2:git://github.com/user-c/dep-2#node0.8.0
That would be awesome. If it's not possible, that would be good to know so I can prepare myself to have to fork/customize the chain of projects.
NPM install syntax:
npm install (with no args in a package dir)
npm install <tarball file>
npm install <tarball url>
npm install <folder>
npm install [#<scope>/]<name> [--save|--save-dev|--save-optional] [--save-exact]
npm install [#<scope>/]<name>#<tag>
npm install [#<scope>/]<name>#<version>
npm install [#<scope>/]<name>#<version range>
npm i (with any of the previous argument usage)
so you can choose one of these methods to install your modules.
The case of the simplest way to install a specific version is this one:
npm install module#0.0.2
more info:
https://docs.npmjs.com/cli/install

Resources