how to workaround npm “Error: Invalid version: ”0.

2019-09-11 02:19发布

问题:

This question already has an answer here:

  • npm: Why is a version “0.1” invalid? 3 answers

I am trying to build a nodejs package. When I run npm install I get Error: Invalid version: "0.1 message and npm installation fails.

I tried to fix the error manually by replacing "version": "0.1", with "version": "0.0.1", in package.json files in modules directories but there are many modules that contain invalid 0.1 version. It's very hard to fix it manually.

Is there a simpler way to fix it? Or maybe an awk, sed or other bash script that search for package.json files recursively and replace "version": "0.1", with "version": "0.0.1", help?

EDIT: I already checked out this thread npm: Why is a version "0.1" invalid? and lots of others prior to asking question

回答1:

find "dir" -type f -name package.json -print |
xargs sed -i 's/"version": "0.1"/"version": "0.0.1"/'

should do what you describe. Replace "dir" with whatever your real starting directory is and test it first of course.



回答2:

Use jq:

jq '.version |= if . == "0.1" then "0.0.1" else . end' package.json

Since in-place editing is not yet available in released versions of jq, combining this with find to process all package.json files in a directory tree requires a subshell to redirect the jq output to a temporary file. For example:

find . -name package.json -exec bash -c "jq '.version |= if . == \"0.1\" then \"0.0.1\" else . end' {} > {}.new && mv {}.new {}" \;