How to install a previous exact version of a NPM package? - node.js

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

Related

How to properly upgrade npm on debian when installed with apt

I have issue with jessie and npm.
I have installed package nodejs with sudo apt-get install nodejs, so it installed npm automatically.
I then want to install latest of npm using npm i -g npm#latest, which work fine except the apt npm version is conflicting.
I want to remove the conflicting apt installed version, or to know the official way to update npm, if installed from apt.
I am only able to update node using npm i -g n then n stable but it is impossible for me to upgrade npm
npm is distributed with Node.js- which means that when you download Node.js, you automatically get npm installed on your computer.
A quote from The npm Blog
The best way to install npm is to install node using the node.js installer. npm is installed as part of node.
It’s over at nodejs.org. It will give you a recent, working version of npm with all the paths in the expected places. This is the version that npm Inc and the Node.js project both support.
Once you’ve installed Node.js, you can make sure you’ve got the very most recent version of npm using npm itself by running this:
npm install npm -g
If you have issues, do not use sudo, read in documentation fixing npm permissions carefully and configure npm properly.

npm WARN npm npm does not support Node.js v9.1.0

I updated to the latest node v9.1.0 and now npm doesn't work.
npm WARN npm npm does not support Node.js v9.1.0
It turns out that the current installed npm is not compatible with the new node and has a hard time updating.
The solution is to uninstall npm and then reinstall node which will contain the correct version of npm.
Here's how:
Uninstall npm.
Mac:
sudo npm uninstall -g npm
Windows:
npm uninstall -g npm
Install node from https://nodejs.org
Uninstall the global instance of npm:
npm uninstall -g npm
This will cause your system to fall back on the version of npm installed by node 9.1.0 and the warning will disappear.
What worked for me was to delete npm and npm cache from C:\Users\chrisd\AppData\Roaming
Then I ran npm i -g npm#next for the latest version of npm for Node 9.x
NPMJS.org has issued a new release 11.27.2017 -- npm v5.6.0
(You may not have to delete the npm/npm cacheif you just upgrade, but I had already done that before finding the new release)
NPMJS Release notes
Fixed my issue update to 5.71
npm uninstall -g npm
npm i -g npm#next
Had similar issue in linux/ubuntu, below helped solving it.
Remove existing versions of npm and node:
sudo apt --auto-remove purge npm
sudo apt --auto-remove purge nodejs
Re-install node
for me worked
npm uninstall -g npm
npm i -g npm#next
No need for manual installation or clearing the cache.
Run with admin credentials if needed.
I saw this warning after I upgraded to Node 10.4.1 after that I just ran npm uninstall -g npm with -g flag to remove it globally and the error was gone. I guess it updated my npm to version 6.1.0
Solution :
Go to the path where you can find the debug log (this file is found in your npm-cache folder) C:\Users\yourname\AppData\Roaming
Delete the NPM and NPM-Cache folder, but DO NOT reinstall node. Once deleted go back to your command line and re-use the command " npm install -g npm#latest "
What worked for me - On Ubuntu
Install nodejs
sudo apt install nodejs
Install npm
curl -L https://www.npmjs.com/install.sh | sudo sh
If you are unable to uninstall npm, instead you can skip that step and just
Reinstall npm https://nodejs.org/en/
This error occurs when the npm version not compatible with the node version. However npm uninstall -g npm also gives the same error for me.
So following steps work for me.
Option 1:
Step1: Follow this directory
C:\Users(your username)\AppData\Roaming
Step2: Delete the npm folder. And if there is a folder called `npm cache, delete it also.
Step3: Run npm clean cache —force ( — force is now required to clean cache)
If the above option is not worked for you please follow the below steps.
Option 2:
Follow this directory
C:\Users(your username)\AppData\Roaming
Delete the npm folder and if there is one mom cache folder.
Run npm clean cache --force ( -- force is now required to clean cache)
Make sure everything to do with Nodejs is deleted and uninstalled.
Reinstall Nodejs.
I had the same issue with npm when I have updated Node js on windows. I tried many things but only working solution was :
First uninstall npm with below command
npm uninstall -g npm
Now Install latest version
npm install npm#latest -g
If you are using Mac and brew, that's how I've got rid of this problem:
Even though I installed and reinstalled node with brew, with last version 11.8.0, apparently it kept using the last version
fdecollibus$ npm install --global gatsby-cli
npm WARN npm npm does not support Node.js v11.8.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8, 9.
npm WARN npm You can find the latest version at https://nodejs.org/
/usr/local/Cellar/node/11.8.0/bin/gatsby -> /usr/local/Cellar/node/11.8.0/lib/node_modules/gatsby-cli/lib/index.js
+ gatsby-cli#2.4.8
npm -v gave me back version 5.6.0. I've therefore noticed that /usr/local/lib/node_modules had wrong permissions assigned to root:wheel.
A chown did not fix the problem: I've had to manually delete the content of the folder (cd /usr/local/lib/node_modules and -careful - type rm -rf * ) to get rid of the folder.
Then I've run brew uninstall node and again brew install node, and finally npm -v gave me back 6.5.0
Executing npm uninstall -g npm gave me same error
Uninstall Node.js installed in the system
Goto c:\Program File\nodejs (default location of the nodejs installation). Remove all contents of the folder manually.
Remove npm, npm-cache folder from Roaming folder under your username.
Install Node.js again.
running npm install #angular/cli -g worked for me.
I was getting the below warning when I executed, npm install to install package.json for my angular project.
npm WARN npm npm does not support Node.js v9.1.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8.
npm WARN npm You can find the latest version at https://nodejs.org/
WARNING: You are likely using a version of node-tar or npm that is incompatible with this version of Node.js.ng-080b84bf08dd68e5.lock for ...\Angular\node
Please use either the version of npm that is bundled with Node.js, or a version of npm (> 5.5.1 or < 5.4.0) or node-tar (> 4.0.1) that is compatible with Node.js 9 and above.
C:\Program Files\nodejs\node.exe: src\node_zlib.cc:437: Assertion `args.Length() == 7 && "init(windowBits, level, memLevel, strategy, writeResult, writeCallback," " dictionary)"' failed.
According to #Ilyich, I uninstalled global npm, npm uninstall -g npm
And then I executed npm install to install my package, it installed successfully.
Navigate to the Appdata by typing %AppData% in the run or search.
Delete the npm and npm-cache folder and then install the nodejs again. It solved the issue for me.
If you are getting the same warnings while creating or importing an Angular project in an IDE such as Eclipse ,do the following:
File->New->Angular
In the window that appears change the versions of Nodejs and NPM to "use system installation"
...
If the process crashes at "installing packages for tooling via npm"
Close the terminal in IDE and manually go to the project directory and do "npm install".
Refresh the project in IDE
I had a similar issue trying to install other stuffs.
And looks like the npm version was not compatible with my node version.
In my case i had npm 5.4.2 and node v10.15.0
At the end of this process i had the version npm 6.7.0 and node v10.5.0
First Option:
I downgrade the version of node and other tools.
On terminal execute the next command with the version to be installed. (Example if we need change to node 8.14.1)
1.- Npm install -g node#8.14.1
Then we execute the command to uninstall the version to be removed.
2.- Npm uninstall -g node#8.15.0
After that we need execute the next command to set the new path
3.- Npm update -g node#8.14.1
Finally we only need execute the version to see if the changes were added successfully
4.- Node -v
In this case we will see the version as 8.14.1 and we finished the process.
If you want do it for Appium and Npm version you can open a new terminal and follow the same steps in order to change the version. For example:
Npm install -g appium#1.7.2
Npm uninstall -g appium#1.10.0
Npm update -g appium#1.7.2
Appium -v
In my case you must have the next versions:
Node 8.14.1
Npm 6.4.1
Other Way:
To solve it i just execute in the terminal the next line.
curl -L https://npmjs.org/install.sh | sh
After download a few files (took around of 10 minutes to see results in the terminal) i was able to continue working with the new latest and compatible version of both tools.
The next WARM was displayed in this case.
npm WARN npm npm does not support Node.js v10.15.0.
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8.
npm WARN npm You can find the latest version at https://nodejs.org/
/Users
I am on a macOS and what ended up working for me was uninstalling npm, relinking Node, and reinstalling Node through Homebrew. (Prereqs: Have Homebrew installed and have a very confusing setup where npm and Node don't work together)
1. To uninstall homebrew:
If you are on a Mac, you probably don't see the AppData\Roamingfolder; you should instead go to npm root -g and delete the npm_modules folder. Now, when you type npm --version your computer should be confused.
2. Reinstalling node:
Try doing brew install node and if you already have node installed, your computer will ask you to reinstall it. If you do brew reinstall node and your versions of brew and npm look good, then you don't need to do the last step!
3. If you need to link the correct version of Node:
This step was something I think was my fault since I think I had installed Node through Homebrew as well as the actual online download. Follow the suggestions after brew link node which should take you to brew link --overwrite node. There will be around 7 directories that the command will not be able to overwrite, which you should all delete using sudo rm -rf directory_name. (Do use your common sense, don't delete something important.) After you are finally able to run the brew link --overwrite node command without any errors, you should then do brew reinstall node since you don't have npm anymore.
Tada! You hopefully now have version of npm and node that hopefully can work together.
Just install latest version of Node.js from https://nodejs.org
This will automatically update npm for you
Easiest way I found was to install the latest node with brew and the latest npm manually:
brew uninstall node
brew install node
npm install -g npm#latest
This removed the warning message. For some reason brew wasn't linking the new npm installed by node while it was updating node itself.
The easiest way(for windows) is to use the installer to remove node, then use it to reinstall node, at which time it will install the correct version of npm. running the above uninstall commands does not work, since the wrong version of node is installed, otherwise you wouldn't have this issue.
You can uninstall node current version, and install a early version that support npm. Then run npm install npm#latest -g and install current node version.
It's work for me
lowering the npm version worked for me (windows 10):
npm i npm#6.14.11
To upgrade the npm and Node:
Step 1. Uninstall the npm:
Mac: `sudo npm uninstall -g npm`
Windows: `npm uninstall -g npm`
Step 2: Install the latest version of Node: https://nodejs.org/en/

How to use a specific version of NPM?

How can I switch which version of npm I'm using?
Currently:
$ npm -v
1.1.65
But I need: 1.0.x
I tried but got an error:
$npm version 1.0
npm ERR! version No package.json found
Anyone know how to use a different version of NPM? Thanks
Your NPM version is tied to your NodeJS version. As far as I can tell you can only have one NPM version per Node version. Using something like nodenv or, my favorite, asdf, you can define your node version per folder.
Per Node version (e.g. per folder) you globally install the version of NPM that you want to use.
$ npm install -g npm#x.x
Edit: You can also now specify NPM version in your package.json "engines" key, though it takes a little more work to enforce that declaration.
You can update npm without installing another version of node.js and npm is not tied to versions of node.js specifically.
Of course, as node.js advances and adds features so too does npm so there is some limit for npm depending on node.js but you SHOULD update npm as much as you can because there are important security patches and bug fixes.
This is a best practice for all software. Stability is not worth ignoring a security risk.
The command is npm install npm#latest -g to install it globally. This will install the latest version that will run with the node.js you have installed.
Additionally you can install a specific version of npm to your package.json in a project like this npm install npm#6.14 and you can use it locally. What is even more interesting is you can install a local version of node.js in a project and use it too!
Example:
I am running node 16.8.0 and I have npm 7.21.0 but I want to use an earlier version of npm but just for a project:
In the project directory
npm i --save-dev npm#6.14
This will put the earlier version of npm into the node_modules/.bin which will let you run it in relation to this project. If you type npm -v at this point you will get your global version.
If you type node_modules/.bin/npm -v you will get 6.14.15
Since you have made this version of npm the one for this project, when you use npm in your scripts it will use 6.14.15 and not the global version.
The same is true of node.js.
You can install an older version of node.js like this: node_modules/.bin/npm install --save-dev node#lts which will then add this version of node.js to the project.
You can test this by entering node_modules/.bin/node -v and in my example you will see the locally installed version number, in my example v14.17.3 although my global is v16.8.0
If you do this, your project scripts in package.json scripts will run the locally installed versions rather than the global versions.
You can test this out by creating a script in your package.json.scripts like this: "what:version": "npm -v && node -v",
Then if you run npm run what:version you will get
> what:version
> npm -v && node -v
6.14.15
v14.17.3
What value is this?
It is a way for you to package node and npm with your project. This can be especially valuable for large projects where you have many people working on the same project, so you have consistency across the board or if you have a build server that has a specific version of node + npm. Additionally if you have corporate requirements, etc.
Additionally there is npx which allows you to do so much more. Using npx you can even try something out without installing it.
Here is the documentation for npm: https://docs.npmjs.com/about-npm-versions
If you find you are needing to switch between different versions of node.js and npm you might want to use nvm https://docs.npmjs.com/downloading-and-installing-node-js-and-npm
When you install npm you only get the latest package, so you need to install specific versions individually, e.g:
npm install -g npm#5.6.0
Once you've done that, run a version check and you should see the version you've just installed:
npm -v
5.6.0
You can install specific version by bellow command.
npm install -g npm#6.14.11
For a specific version, use
npm install -g npm#x.y.z
And for the latest version, use
npm install -g npm#latest

npm install vs. update - what's the difference?

What is the practical difference between npm install and npm update? When should I use which?
The difference between npm install and npm update handling of package versions specified in package.json:
{
"name": "my-project",
"version": "1.0", // install update
"dependencies": { // ------------------
"already-installed-versionless-module": "*", // ignores "1.0" -> "1.1"
"already-installed-semver-module": "^1.4.3" // ignores "1.4.3" -> "1.5.2"
"already-installed-versioned-module": "3.4.1" // ignores ignores
"not-yet-installed-versionless-module": "*", // installs installs
"not-yet-installed-semver-module": "^4.2.1" // installs installs
"not-yet-installed-versioned-module": "2.7.8" // installs installs
}
}
Summary: The only big difference is that an already installed module with fuzzy versioning ...
gets ignored by npm install
gets updated by npm update
Additionally: install and update by default handle devDependencies differently
npm install will install/update devDependencies unless --production flag is added
npm update will ignore devDependencies unless --dev flag is added
Why use npm install at all?
Because npm install does more when you look besides handling your dependencies in package.json.
As you can see in npm install you can ...
manually install node-modules
set them as global (which puts them in the shell's PATH) using npm install -g <name>
install certain versions described by git tags
install from a git url
force a reinstall with --force
npm install installs all modules that are listed on package.json file and their dependencies.
npm update updates all packages in the node_modules directory and their dependencies.
npm install express installs only the express module and its dependencies.
npm update express updates express module (starting with npm#2.x, it doesn't update its dependencies).
So updates are for when you already have the module and wish to get the new version.
In most cases, this will install the latest version of the module published on npm.
npm install express --save
or better to upgrade module to latest version use:
npm install express#latest --save --force
--save: Package will appear in your dependencies.
More info: npm-install
npm update: install and update with latest node modules which are in package.json
npm install: install node modules which are defined in package.json(without update)
Many distinctions have already been mentioned. Here is one more:
Running npm install at the top of your source directory will run various scripts: prepublish, preinstall, install, postinstall. Depending on what these scripts do, a npm install may do considerably more work than just installing dependencies.
I've just had a use case where prepublish would call make and the Makefile was designed to fetch dependencies if the package.json got updated. Calling npm install from within the Makefile would have lead to an infinite recursion, while calling npm update worked just fine, installing all dependencies so that the build could proceed even if make was called directly.

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