How to install compatible dependencies versions for NodeJs using NPM - node.js

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/

Related

Difference between `npm i` and `npm i <package name>`

In my React Native project, I have a given version of react in my package.json, but if I run npm i react, it will install the latest version of react. If I run npm i though, will it install all the packages in my package.json, with the versions given in the package.json? I don't want it to install the latest versions of all the packages, so I'm scared to try it without asking.
You should go through documentation before getting into anything :)
Using npm i will install whatever is stated in the package.json. If the package.json binds the packages to versions, then it will install that specific version. Most often, package.json would use the ^ character before the package version, which stands for any version above the mentioned version, and if that is the case for you, yes, npm i will install the latest version of those packages.

NPM install package compatible with specific version of another package

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.

Clarification of the --save option for npm install

First experiences with node.js/npm. From the npm-install docs I read:
npm install takes 3 exclusive, optional flags which save or update the package version in your main package.json:
--save: Package will appear in your dependencies.
--save-dev: Package will appear in your devDependencies.
--save-optional: Package will appear in your optionalDependencies.
But I can't understand how it works in practice. If, for example, I run the command:
npm install bower --save-dev
I'd expect to find a package.json file in the current directory with devDependencies set to the installed version of bower, instead I find nothing.
Am I doing/expecting something wrong?
Using node v0.10.21, npm 1.3.12 on Ubuntu 12.04 x64
npm won't create package.json for you, but it will create the necessary dependencies for you as long as package.json exists and is legal JSON.
Create it like so
echo {} > package.json
Then, doing npm i --save whatever will add whatever#~x.x.x as a dependency as expected. The file needs to be there, and be JSON, that's it.
npm install only fetches the packages from the registry and puts them in your ./node_modules. It updates your package.json to register this new dependency if you tell it to.
Your package.json has three dependency blocks :
dependencies - these are needed for your app to run.
devDependencies - these are needed for the developer environments for your app (this is how your teammates can get the packages that you recently added to the project. The dependencies listed here are not required on production hosts.)
optionalDependencies - These packages are optional and it is OK if npm cant reolve the package to install. i.e a build failure does not cause npm install to fail (Note however, that your app should handle cases where the package cannot be found.)
Here is the behavior with the different usages of the npm install command:
$ npm install async #Only installs, no change made to package.json
$ npm install async --save #Installs, adds async#version to dependencies block
$ npm install async --save-dev # Installs, adds async#version to the devDependencies block
$ npm install async --save-optional # Installs, adds async#version to the optionalDependencies block

What does -save-dev mean in npm install grunt --save-dev

I've just started using Grunt.js. It is pretty hard to set up and I am at the point of creating a package.json file.
Following this tutorial, it says there are 3 ways to create a package.json file.
The first is to do npm install grunt --save-dev
But what does --save-dev means? I tried looking but it ends in vain.
--save-dev: Package will appear in your devDependencies.
According to the npm install docs.
If someone is planning on downloading and using your module in their program, then they probably don't want or need to download and build the external test or documentation framework that you use.
In other words, when you run npm install, your project's devDependencies will be installed, but the devDependencies for any packages that your app depends on will not be installed; further, other apps having your app as a dependency need not install your devDependencies. Such modules should only be needed when developing the app (eg grunt, mocha etc).
According to the package.json docs
Edit: Attempt at visualising what npm install does:
yourproject
dependency installed
dependency installed
dependency installed
devDependency NOT installed
devDependency NOT installed
devDependency installed
dependency installed
devDependency NOT installed
There are (at least) two types of package dependencies you can indicate in your package.json files:
Those packages that are required in order to use your module are listed under the "dependencies" property. Using npm you can add those dependencies to your package.json file this way:
npm install --save packageName
Those packages required in order to help develop your module are listed under the "devDependencies" property. These packages are not necessary for others to use the module, but if they want to help develop the module, these packages will be needed. Using npm you can add those devDependencies to your package.json file this way:
npm install --save-dev packageName
To add on to Andreas' answer, you can install only the dependencies by using:
npm install --production
When you use the parameter "--save" your dependency will go inside the #1 below in package.json. When you use the parameter "--save-dev" your dependency will go inside the #2 below in package.json.
#1. "dependencies": these packages are required by your application in production.
#2. "devDependencies": these packages are only needed for development and testing
Documentation from npm for npm install <package-name> --save and npm install <package-name> --save-dev can be found here:
https://docs.npmjs.com/getting-started/using-a-package.json#the-save-and-save-dev-install-flags
A package.json file declares metadata about the module you are developing. Both aforementioned commands modify this package.json file. --save will declare the installed package (in this case, grunt) as a dependency for your module; --save-dev will declare it as a dependency for development of your module.
Ask yourself: will the installed package be required for use of my module, or will it only be required for developing it?
For me the first answer appears a bit confusing, so to make it short and clean:
npm install <package_name> saves any specified packages into dependencies by default. Additionally, you can control where and how they get saved with some additional flags:
npm install <package_name> --no-save Prevents saving to dependencies.
npm install <package_name> ---save-dev updates the devDependencies in your package. These are only used for local testing and development.
You can read more at in the dcu
–save
The package installed is core dependency.
–save-dev
The package installed is not a core rather development dependency.
Use only when developing
--save-dev means omit in production environments, use only in development environments (smaller, and probably faster).

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