0

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

Community
  • 1
  • 1
kenn
  • 328
  • 15
  • 27

2 Answers2

2
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.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • You are quite brave to bet that all those JSON files are formatted just the right way. – Wintermute Apr 19 '15 at 22:48
  • No, the script just give the OP what (s)he asked for, i.e. `an awk, sed or other bash script that search for package.json files recursively and replace "version": "0.1", with "version": "0.0.1"`. If that's NOT what's actually needed, as with all Q&A here, it's up to the OP to figure out what is. – Ed Morton Apr 20 '15 at 00:21
  • 1
    @EdMorton Your solution works but I failed to build the node package due to different errors. So I mark this question as fixed. – kenn Apr 20 '15 at 09:34
1

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 {}" \;
Wintermute
  • 42,983
  • 5
  • 77
  • 80