How to fix - npm ERR! Invalid version

Ever get npm ERR! Invalid version when doing a npm install? This guide includes steps to fix this!

Feb 22, 2023 | Read time 11 minutes

🔔 Table of contents

Introduction

When I first started out as a software engineer, I didnt really pay attention to versioning that much. So for one of my first projects that I worked on, I downloaded the code with git clone and then tried to run it with npm install.

Ok great! I found weekendprojects with the following error:

npm ERR! Invalid version

A more verbose error message could look something like this:

$ npm install
npm ERR! install Couldn't read dependencies
npm ERR! Error: invalid version: 0.1
npm ERR!     at validVersion (/usr/local/Cellar/node/0.10.5/lib/node_modules/npm/node_modul
es/read-package-json/read-json.js:571:40)
npm ERR!     at final (/usr/local/Cellar/node/0.10.5/lib/node_modules/npm/node_modules/read
-package-json/read-json.js:323:23)
npm ERR!     at /usr/local/Cellar/node/0.10.5/lib/node_modules/npm/node_modules/read-packag
e-json/read-json.js:139:33
npm ERR!     at cb (/usr/local/Cellar/node/0.10.5/lib/node_modules/npm/node_modules/slide/l
ib/async-map.js:48:11)
npm ERR!     at /usr/local/Cellar/node/0.10.5/lib/node_modules/npm/node_modules/read-packag
e-json/read-json.js:301:48
npm ERR!     at fs.js:207:20
npm ERR!     at Object.oncomplete (fs.js:107:15)
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>

npm ERR! System Darwin 12.3.0
npm ERR! command "/usr/local/Cellar/node/0.10.5/bin/node" "/usr/local/bin/npm" "install"
npm ERR! cwd /Users/weekendprojects/Documents/ply/dev-server
npm ERR! node -v v0.10.5
npm ERR! npm -v 1.2.18
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/weekendprojects/Documents/ply/dev-server/npm-debug.log
npm ERR! not ok code 0

Or if we inspect the npm-debug.log, we can see something similar to this:

43 verbose stack TypeError: Invalid Version: 
43 verbose stack     at new SemVer (C:\Users\weekendprojects\AppData\Roaming\npm\node_modules\npm\node_modules\semver\classes\semver.js:38:13)
43 verbose stack     at compare (C:\Users\weekendprojects\AppData\Roaming\npm\node_modules\npm\node_modules\semver\functions\compare.js:3:32)
43 verbose stack     at Object.gte (C:\Users\weekendprojects\AppData\Roaming\npm\node_modules\npm\node_modules\semver\functions\gte.js:2:30)
43 verbose stack     at Node.canDedupe (C:\Users\weekendprojects\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\node.js:1054:32)
43 verbose stack     at PlaceDep.pruneDedupable (C:\Users\weekendprojects\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:465:14)
43 verbose stack     at PlaceDep.placeInTree (C:\Users\weekendprojects\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:326:14)
43 verbose stack     at PlaceDep.place (C:\Users\weekendprojects\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:214:10)
43 verbose stack     at new PlaceDep (C:\Users\weekendprojects\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:71:10)
43 verbose stack     at C:\Users\weekendprojects\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\build-ideal-tree.js:965:31
43 verbose stack     at Array.map (<anonymous>)
44 verbose cwd C:\Users\Dont\Want\To\Reveal\This\Information
45 verbose Windows_NT 10.0.19042
46 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\weekendprojects\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "i" "-force"
47 verbose node v16.14.0
48 verbose npm  v8.5.1
49 error Invalid Version: 
50 verbose exit 1
51 timing npm Completed in 18500ms

Why are we getting this error?

The main cause of the error npm ERR! Invalid version is because we are specifying the wrong version in package.json file, using NPM/Node versions that is not compatible or even corrupted NPM cache.

Steps to fix this error

  1. Make sure that the version number in your package.json file is in the correct format.
  2. Check the version number of the package that you are trying to install.
  3. Check the supported Node/NPM version
  4. Clear out the cache with npm cache clean --force
  5. Reinstall your packages by clearing out node_modules and running npm install
  6. Run with --no-audit flag setting

1. Make sure that the version number in your package.json file is in the correct format.

NPM and Node uses semantic versioning to version their packages. So the first thing that we need to check is to go to our package.json file and check that any version is not valid!

Version numbers needs to have three numbers - the major version, the minor version and the patch version number. An example is:

  • 1.2.6

In the above, we can read it as follows:

  • major version = 1
  • minor version = 2
  • patch version = 6

Now the error comes up when you do NOT follow this format. As an example package.json:

{
  "name": "Name",
  "version": "1.0.0",
  "description": "Desc",
  "main": "app.js",
  "dependencies": {
    "bad-words": "1.0.0.0",
    "cowsay": "0.1",

    ...
  }
}

We can see that there are two issues with the dependencies:

  • "bad-words": "1.0.0.0" contains four numbers instead of three
  • "cowsay": "0.1" - contains two numbers instead of three

What is semantic versioning anyway?

Semantic versioning (https://semver.org/) is the format that NPM adopts to verion its packages.

The format follows the structure of: MAJOR.MINOR.PATCH - all of these should be numbers!

So a version of 2.3.9 means that it is a major version of 2, minor version of 3 and patch version of 9.

Now the main benefit is using operators and ranges for your versions.

  • tilde operator (~) - this means that we want to restrict versions that match the MINOR version
  • caret operator (^) - this means we want versions that match the MAJOR version
  • we can also combine these with ranges eg (1.0.0 - 1.2.9)

Lets take a few examples with the lodash (https://www.npmjs.com/package/lodash) library:

  • ~1.1.0 - this will match versions 1.1.0 and later versions -> 1.1.1
  • ^1.1.0 - this will match all versions under the major version 1

2. Check the version number of the package that you are trying to install.

A common mistake is that we are putting in version that does not exist for the particular NPM package we want to use.

Make sure that it is a valid version number.

As an example lets say we want to use the cowsay NPM package (https://www.npmjs.com/package/cowsay)

Now if we have the following package.json and we have it use version 1.2.1:

 

{
  "name": "Name",
  "version": "1.0.0",
  "description": "Desc",
  "main": "app.js",
  "dependencies": {
    "cowsay": "1.2.4",
    ...
  }
}

The error will come up because 1.2.1 does not exist! To fix this, check the version numbers available here: https://www.npmjs.com/package/cowsay?activeTab=versions

A robust way to fix this is using the ~ operator on a baseline version that your application will use:

 

{
  "name": "Name",
  ...
  "dependencies": {
    "cowsay": "~1.2.4",
    ...
  }
}

3. Check the supported Node/NPM version

A particular scenario where this error comes up this when your appplication is targetting specific node or NPM versions. Consider the below package.json:

{
  "name": "Name",
  "version": "1.0.0",
  "description": "Desc",
  "main": "app.js",
  "engines": {
    "node": "8.7.0",
    "npm": "5.4.2"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js",
  },
  "dependencies": {
    "bad-words": "^1.5.1",
    ...
  }
}

We can see that there is a engines attribute and the application will only run with node 8.7.0 and NPM version 5.4.2

"engines": { "node": "8.7.0", "npm": "5.4.2" },

Targeting Node and NPM versions like this can be problematic when dealing with build or deploy servers. You might have the node and NPM version on your development machine, but when go and deploy/ build the servers might not.

A classic example is when I recently worked on a project with requiring engine node version 8.7.0

  "engines": {
    "node": "8.7.0",
    "npm": "5.4.2"
  },

Now when we go to deploy it on Google Cloud, it throws the error npm ERR! Invalid version. Looking at the Google Cloud documentation, node version 8 is deprecated and not supported anymore.

To fix this we need to update our application to use a supported version of node such as version 18:

{
  "engines": {
    "node": "18.x.x"
  }
}

Tip: Change node versions when on Mac OSX M1

On a few rare occurances, Mac OSX M1 does not play nice with some node/NPM versions.

We can use NVM to manage our node versions!

As an example a recent project I was working on a Mac M1 and found that the npm install will work on node v15.3.0 with npm v7.0.14

4. Clear out the cache with npm cache clean --force

NPM cache clean can be my goto for most of NPM errors!

You might of changed the version to be the correct version and that it is semantically correct, but its still giving you this error of npm ERR! Invalid version.

This can be due to the NPM cache. NPM stores packages and their dependencies in a cache folder so that it saves you time and bandwidth when we are getting the same package again.

This can sometimes get corrupted and so we need to run the cache clean command:

  • npm cache clean --force
  • npm install

5. Reinstall your packages by clearing out node_modules and running npm install

If the previous steps and did not work, we can go the full nuke method and clear everything out and start from scratch:

  1. Delete node_modules (or move it somewhere outside from the project directory)
  2. Delete the package-lock.json file
  3. Run npm install

6. Run with --no-audit flag setting

Summary

In this post, I went over a typical error when starting out with NPM/Node projects npm ERR! Invalid version.

This error is usually caused by incorrect semantic versioning of a dependent package, using a version that does not exist or even using incompatible versions of node and NPM.

To fix this we have to check that our semantic version is correctly set - having three numbers major, minor, and patch, checking that the NPM package we are using have published the version we want.

On some instances such as using Mac OSX M1 chip, we might have to target specific versions of Node/NPM for it to work!

Additionally if the previous options did not work, we can clear everything with npm clear cache or removing node_modules and package-lock.json and install again npm install

References

👋 About the Author

G'day! I am Huy a software engineer based in Australia. I have been creating design-centered software for the last 10 years both professionally and as a passion.

My aim to share what I have learnt with you! (and to help me remember 😅)

Follow along on Twitter , GitHub and YouTube