Solving "npm ERR! code ETARGET No matching version found" Error
Introduction
When working with Node.js, you might have come across the npm ERR! code ETARGET. This error can be quite frustrating, especially when it prevents you from installing or updating your npm packages. However, don't fret! In this article, we'll explore what this error means, and how to solve it.
What is the Error?
The npm ERR! code ETARGET
error typically happens when you're trying to install a version of a package that doesn't exist. Most commonly, this happens when there's a typo in the package name, a type of the versioning syntax, or a problem with your npm cache.
$ npm install [email protected]
npm ERR! code ETARGET
npm ERR! notarget No matching version found for [email protected].
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.
Luckily the error message is actually informative and tells you that the version you're trying to install doesn't exist. In the example above, we were trying to install express
version 4.123.0
, which really doesn't exist.
Checking Available Module Versions
The first step I'd recommend is to check the available versions of the module that you're trying to install. You can do this using the npm show
command:
$ npm show express versions
[
'0.14.0', '0.14.1', '1.0.0-beta', '1.0.0-beta2',
'1.0.0-rc', '1.0.0-rc2', '1.0.0-rc3', '1.0.0-rc4',
'1.0.0', '1.0.1', '1.0.2', '1.0.3',
...
'4.17.2', '4.17.3', '4.18.0', '4.18.1',
'4.18.2', '5.0.0-alpha.1', '5.0.0-alpha.2', '5.0.0-alpha.3',
'5.0.0-alpha.4', '5.0.0-alpha.5', '5.0.0-alpha.6', '5.0.0-alpha.7',
'5.0.0-alpha.8', '5.0.0-beta.1'
]
This command will display all the available versions of the express
module. If the version you're trying to install isn't listed, that means it doesn't exist, and you'll need to install a different version.
Verifying Package Spelling
Let's say you tried the above solution, but got an error like the following:
$ npm show eexpress versions
npm ERR! code E404
npm ERR! 404 Unpublished by undefined on 2019-04-05T17:01:00.748Z
npm ERR! 404
npm ERR! 404 'eexpress' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
(Notice I misspelled "express")
In this case you need to verify the spelling of the package in your package.json
file (or on the command line). Typos happen to all of us, and npm won't be able to find a package that doesn't exist.
Open your package.json
file and check the spelling of the package causing the error. Make sure it matches the official name on npm.
Clearing npm Cache
If your package spelling is correct and the version is in the npm show
command, then the next step is to try clearing your npm cache. The npm cache is where npm stores downloaded packages. Sometimes, this cache can cause problems, especially if it contains outdated or corrupted packages.
$ npm cache clean --force
The above command will forcefully clear your npm cache. Once the cache is cleared, try installing your package again.
Updating the Package
If the version you're trying to install does exist, but you're still getting the error, it might be that you need to update your package. You can do this using the npm update
command.
$ npm update express
This command will update the express
module to the latest version. So instead of relying on you to figure out the exact (and real) version, npm will do it for you. After running this command, your package should be updated, and the error should be resolved.
Removing node_modules and package-lock.json
Another option that might be a good idea is to remove any remnants of the problematic package from your project. This includes the node_modules
directory and the package-lock.json
file.
$ rm -rf node_modules
$ rm package-lock.json
This way we know we're starting from a clean state before we attempt to reinstall the package.
Note: This will remove all installed packages and the lock file. If you have other packages installed that are working fine, you may want to just delete the specific module directory under node_modules
. But clearing both is more likely to solve your problem.
Reinstalling the Package
With the node_modules
directory and package-lock.json
file out of the way, we can now try reinstalling the package.
$ npm install
This command will fetch the packages listed in your package.json
file and install them in a new node_modules
directory. It will also create a new package-lock.json
file. If the problematic package was just having a bad day, this might solve your issue.
Conclusion
The npm ERR! code ETARGET No matching version found
error can be a bit tricky to solve, but with a systematic approach, it's definitely manageable. We've gone through several steps to troubleshoot and fix this error, from clearing the npm cache and checking available module versions, to removing the node_modules
directory and package-lock.json
file, reinstalling the package, and verifying the package spelling. Hopefully, one of these methods worked for you.