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

2019-09-11 02:32发布

This question already has an answer here:

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

2条回答
来,给爷笑一个
2楼-- · 2019-09-11 02:44

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 {}" \;
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-09-11 03:01
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.

查看更多
登录 后发表回答