34

Whenever I run npm install I get

npm ERR! Invalid Version: 

This is the log file.

43 verbose stack TypeError: Invalid Version: 43 verbose stack at new SemVer (C:\Users\Myself\AppData\Roaming\npm\node_modules\npm\node_modules\semver\classes\semver.js:38:13) 43 verbose stack at compare (C:\Users\Myself\AppData\Roaming\npm\node_modules\npm\node_modules\semver\functions\compare.js:3:32) 43 verbose stack at Object.gte (C:\Users\Myself\AppData\Roaming\npm\node_modules\npm\node_modules\semver\functions\gte.js:2:30) 43 verbose stack at Node.canDedupe (C:\Users\Myself\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\node.js:1054:32) 43 verbose stack at PlaceDep.pruneDedupable (C:\Users\Myself\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:465:14) 43 verbose stack at PlaceDep.placeInTree (C:\Users\Myself\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:326:14) 43 verbose stack at PlaceDep.place (C:\Users\Myself\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:214:10) 43 verbose stack at new PlaceDep (C:\Users\Myself\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:71:10) 43 verbose stack at C:\Users\Myself\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\\Myself\\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 

I don't understand what version of what is invalid? Help!

1
  • It would help if you would provide your package.json. Either the whole file – or at least "version": "x.x.x" and everything under "dependencies". Commented Apr 20, 2022 at 14:08

8 Answers 8

53

Deletingnode_modules and package-lock.json (OR yarn.lock) solved the issue for me.

Sign up to request clarification or add additional context in comments.

7 Comments

thx! Sometimes you just wanna make it work without digging deeper and trying to understand all the nuances about npm.
thanks, just deleting the package.lock worked for me.
OK, it works to delete the package-lock.json, the question is why? How to know where the error comes from? I am getting error Invalid Version: ^5.0.0 in an Angular 13 app. But why? Which package? I don't see it in the logs. Must be this in package.json: "resolutions": { "webpack": "^5.0.0" }
If you don't want to delete your lock file: Open the lock file, and delete all occurrences of the package npm complains about. Save the lock file and run npm install again.
rm -f node_modules rm package-lock.json npm cache clean --force
|
13

Delete package-lock.json and node_modules/.package-lock.json, then run the npm install command again.

This solves the problem for me without having to delete and re-download all the modules that were installed correctly.

1 Comment

This actually solved my problem. I didn't have to delete the whole node_modules folder
7

Solution

Delete the node_modules folder, remove package.lock and clean cache:

rm -rf node_modules/ npm cache clean 

If still not working, you want to take the risk of breaking changes, this will do it

rm package-lock.json npm cache clean --force 

Re-install

npm install 

or simply

npm i 

3 Comments

I would like to ask why this works.
rm -f node_modules rm package-lock.json npm cache clean --force
deleting the lock file is not recommended, you may unintentionally break something
2

I had the same error with [email protected]. Fixed by downgrading to [email protected].

2 Comments

Actually you upgraded the npm version not downgrade it.
@SaeedHemmati 8.5 is lower than 8.12.
1

Solution : if you check your "package.json" file in your projects root, it's probably missing one or all of the following properties:

  1. name
  2. version

for version, it has to be in the form "x.x.x", i.e in my(and probably yours) case "1.0.0"

for name you can obtain it from the "app.json" for example in my case:

{ "expo": { "name": "the-10-min", "slug": "the-10-min", "privacy": "public", "platforms": [ "ios", "android", "web" ], 

so in my case the name property in the "package.json" file will be "the-10-min" .Plug in the values for name and version then run the command "npm install"

Comments

0

You should also ensure that the name properties in app.json and package.json are the same- they were different in mine and caused the error

Comments

0

Ran into a similar problem after cloning an old project. Tried all of these solutions, none worked. Turns out my problem was due to deprecated package versions. I removed them all and added them gradually until I figured out which were the ones giving me problems. Luckily, The newer versions of these packages were ok.

Comments

0

Try to remove the "version" key from your package.json file.

For example, your package.json may look like this:

{ "name": "app_name", "version": "1.0.0", "private": true, // rest of file } 

Remove the "version" line so it becomes:

{ "name": "app_name", "private": true, // rest of file } 

Removing the version number forces npm to use the latest available version of each dependency instead of trying to install a specific one. This can resolve errors where npm tries to install an invalid or unavailable version.

Let me know if the error persists after removing the version number, as there could be other causes. But this solution works for me.

3 Comments

@Yunnosch got it <>
Oh sorry forgot to edit my answr will do it right now
Thank you for the feedback and tips on improving my answer! I have updated my answer to follow the formatting guidelines better and provide more clarity. Please see the revised version @Yunnosch

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.