Difference between `npm i` and `npm i <package name>` - node.js

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.

Related

"npm install package#version" does not actually install that version

When I try to install a specific version of a package, it doesn't do it.
npm install express#4.16.0
...installs...no errors
npm view express version
4.18.2
I have no clue why this would be happening. I understand the modifiers in package.json, but that's not this, this is me manually trying to install a specific version.
Any ideas?

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 package has outdated dependency, is there a way to alter its version?

Just migrated to Node 4.1.2 from 0.10. One of the packages being installed via npm install errors due to node-gyp having a problem with one of its dependencies, it's quite a few versions out of date. The issue has been brought up on the repo but hasn't seen activity since May. Is there a way to tell NPM to install this package but with the outdated dependency using a newer version?
EDIT:
I've copied over an installed version from node_modules in an older project. npm install -g npm-check-updates then ncu in the node_modules/bs-html-injector/ directory. It lists updates, ncu -u will update the package.json, npm install after. I run my gulp task and html is injecting fine, all seems fine :) Would still like to know how to do this if I didn't have a local copy installed by NPM. It looks like it's just a 1:1 copy from the github repo?
With npm, you can install packages from GitHub directly:
npm install user/repo#branch
You can fork the package on GitHub, make and propose the changes you need and use your fork as a dependency in your project until PR is merged.
you can use --force to force install it

Make `npm install --save` add a strict version to package.json

When you run npm install --save somepackage, it usually adds something like this into package.json:
"dependencies": {
"somepackage": "^2.1.0"
}
Because the version is prepended with a caret(^), this means that if you later run npm install, it might install version 2.3.0 instead. This can be undesirable for fairly obvious reasons. npm shrinkwrap is useful, but doesn't really solve the problem.
So, I have several questions:
When installing a package, is it possible to specify that you want it to be set to a specific version in package.json (no caret before the version number)?
When publishing a package to npm, is there any way to prevent the default of including the caret before the version when other developers install your package?
To specify by default a exact version, you can change your npm config with save-exact:
npm config set save-exact true
You can also specify the prepend version with a tilde with save-prefix.
And, no you can't force user to update to a minor or a patch version, NPM uses semver and it's the recommend way of publishing packages.
You can change the default behaviour by using the --save-exact option.
// npm
npm install --save --save-exact react
// yarn
yarn add --exact react
I created a blog post about this if anyone is looking for this in the future.
https://www.dalejefferson.com/blog/how-to-save-exact-npm-package-versions/
Run:
npm install --save --save-exact my-module#my-specific-version
Adding an answer to make this advice easier to see.

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).

Resources