Does npm install package twice? - node.js

I run npm install to install my dependencies.
At the first time, some package install failed.
I ran npm install again.
Will npm reinstall every dependency again or just the package not be installed?

Running npm install apart from the first time installs only the missing packages and those that need update.
As per stian's input, refer docs.npmjs.com/cli/install#algorithm

Running npm install after the first time only installs missing packages or updates existing ones.

Related

Using --ignore-scripts for one dependency in NPM

Following this question, NPM dependencies can be installed using:
$ npm install --ignore-scripts
Is there a way to mark that a dependency should be installed without running scripts in package.json?
This is because, when I run npm install --ignore-scripts, the dependency is added to package.json. As a result, other users will install the package while running scripts, however I want this certain package to never run scripts.
I could be wrong but I believe its: npm install -ignore-script package-name#version

NPM Install is not installing dependencies

I'm attempting to install the Ushahidi V3 Client. I've been following the install process up until when I need to build the project from the source repo using npm and gulp - both of which I've had zero experience with. Whenever I run sudo npm install in the project directory, the process runs without complaints. However, when I run npm ls to verify that dependencies have been downloaded, I get a bunch of dependencies listed out as being missing.
How do I get npm to resolve all of these dependencies?
System Details
OS Ubuntu 14.04 (Trusty)
Node JS v0.12.9
NPM v3.5.1
What I've tried
Removing node_modules folder and re-running sudo npm install as referenced in this SO answer for a similar question: npm Gulp dependencies missing, even after running npm install
Uninstalling and reinstalling node and npm
#Strainy, as your research :D
It was a combination of running as sudo and not having the build-essentials.
That's why you should not use sudo npm
Follow these steps:
try npm uninstall. and then try npm install.
Also If it still doesn't work.
Try:
npm install -g npm-install-missing
or
npm-install-missing
For further reading, click here.

"npm install" independent of npmjs.org

npm install downloads packages from npmjs.org, compiles and then installs. So even if the node_modules folder is taken backup it can not be used on other machines where the os might be different, due to the native machine code generated during the npm install.
Also another problem with npm install is that it downloads from npmjs.org. What if the site goes down?. How to download the packages and the same package be installed offline on all platforms?
You can tell npm to use a mirror if npmjs.org is down. For example:
npm set registry http://registry.npmjs.eu/
Or with a runtime option:
npm --registry http://registry.npmjs.eu/ install express
If you do keep the node_modules directory with your code, you can simply run npm rebuild to re-compile anything that needs it.
Otherwise, you have a lot of options for installing from various locations. You could keep package tarballs locally. From the documentation:
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)

How to install a previous exact version of a NPM package?

I used nvm to download node v0.4.10 and installed npm to work with that version of node.
I am trying to install express using
npm install express -g
and I get an error that express requires node version >= 0.5.0.
Well, this is odd, since I am following the directions for a node+express+mongodb tutorial here that used node v0.4.10, so I am assuming express is/was available to node v0.4.10. If my assumption is correct, how do I tell npm to fetch a version that would work with my setup?
If you have to install an older version of a package, just specify it
npm install <package>#<version>
For example: npm install express#3.0.0
You can also add the --save flag to that command to add it to your package.json dependencies, or --save --save-exact flags if you want that exact version specified in your package.json dependencies.
The install command is documented here: https://docs.npmjs.com/cli/install
If you're not sure what versions of a package are available, you can use:
npm view <package> versions
And npm view can be used for viewing other things about a package too. https://docs.npmjs.com/cli/view
It's quite easy. Just write this, for example:
npm install -g npm#4.6.1
Or:
npm install -g npm#latest // For the last stable version
npm install -g npm#next // For the most recent release
First remove old version, then run literally the following:
npm install express#3.X
or
npm install express#4.X
and for stable or recent
npm install -g npm#latest // For the last stable version
npm install -g npm#next // For the most recent release
In my opinion that is easiest and fastest way:
$ npm -v
4.2.0
$ npm install -g npm#latest-3
...
$ npm -v
3.10.10
you can update your npm package by using this command:
npm install <package_name>#<version_number>
example:
npm install yargs#12.0.2
You can use the following command to install a previous version of an npm package:
npm install packagename#version
I have a general way to solve this type of problems, which could be helpful too, especially when cloning repositories to run them locally, but requires a little more analysis of the versions.
With the package npm-check-updates I verify the versions of the packages (according to the package.json file) that are not declared in their latest available versions, as shown in the figure (https://www.npmjs.com/package/npm-check-updates):
With this information we can verify the update status of the different packages and make decisions as to which packages to upgrade / degrade and which ones do not.
Assuming that we decided to update all the packages as they are listed, we can use the ncu -u command which only modifies your package.json file. Run npm install to update your installed packages and package-lock.json.
Then, depending on the requirements of the repository, we can refine what is needed, installing the specific versions with
npm view <package> versions and npm install <package>#<version>
The easiest way I found: add package name with the version in package.json and then run npm install
"next-seo": "^5.4.0",
"next-themes": "^0.1.1",
"nextjs-progressbar": "^0.0.14",
If you have to install an older version of a package, just specify it
npm install #
For example: npm install express#3.0.0
You can also add the --save flag to that command to add it to your package.json dependencies, or --save --save-exact flags if you want that exact version specified in your package.json dependencies.
The install command is documented here: https://docs.npmjs.com/cli/install
If you're not sure what versions of a package are available, you can use:
npm view versions
And npm view can be used for viewing other things about a package too. https://docs.npmjs.com/cli/view
Use npm config set save-exact=true if you want to install the exact version

How do I update devDependencies in NPM?

npm update seems to just update the packages in dependencies, but what about devDependencies.
Right now you can install devDependencies by running npm install ., but this doesn't work for npm update .
Any ideas?
To update package.json in addition to the local modules, run
npm update --save-dev
Alternatively, the same command to save time
npm update -D
You can view the full detail of update, or any command for that matter through
npm help <cmd>
Install npm-check-updates (https://www.npmjs.org/package/npm-check-updates), then jump into your project folder and run:
npm-check-updates
And to update and save changes to your package.json file:
npm-check-updates -u
These steps worked for me :
npm install -g npm-check-updates
ncu -u
npm update
npm install
npm outdated - for an overview what's outdated
npm install -g npm-check-updates - as pointed correctly by Michael
ncu -u - it'll automatically update all dependencies (also dependencies, i.e., it's of course different than devDependencies) versions in package.json, without reinstalling it yet. It'll just change the "numbers" in package.json
npm update - actual dependencies installation
(Optional, depending by scenario) you might need to use the flag --force, or (new in NPM v7) --legacy-peer-deps to complete the process. You can read about difference between those 2 on What does npm install --legacy-peer-deps do exactly? When is it recommended / What's a potential use case?
(Optional) you can validate it using ncu -u and for correctly updated dependencies you should see the text All dependencies match the latest package versions :)
This problem does no longer excise with the current version of NPM (1.3.11).
Update works fine with: npm update
If you are using outdated npm version it might be the problem. So before any other commands execute:
sudo npm install npm -g
or (if above doesn't work):
sudo npm update npm -g
Then relaunch the console (in order for changes to take effect).
Now you can check your new npm --version and if it is up to date execute:
npm update
or (if you prefer):
npm update --save-dev
I ran into the same problem as OP had, and found no solution, so I decided to write a Grunt plugin that will auto-update my devDependencies..
It's on Github, I'd love to get some input and collaborations in order to make it the best tool that NPM hasn't provided.
Basically it will auto-update your outdated development dependencies with a simple Grunt Task.
https://github.com/pgilad/grunt-dev-update
What worked for me is installing individual dev dependencies like this
npm install react-test-renderer#15.6.1 --save --only=dev
i found the answer onhttps://nodejs.dev/learn/update-all-the-nodejs-dependencies-to-their-latest-version and this is working for me for all the major release as well
npm install -g npm-check-updates
ncu -u
npm update
to check the outdated package use
npm outdated
One (slow) way to do force the update, is to remove the node_modules directory, and then do npm install again.
This was a known bug of the npm update command, which has been fixed on the development branch of npm, see here:
https://github.com/isaacs/npm/pull/3863
It should land on the latest stable version of npm pretty soon.

Resources