7

I tried installing reactjs via node using two different commands in the terminal using 1) npx create-react-app my-app and 2) npx create-react-app app

But both yielded the same error shown below:

Installing packages. This might take a couple of minutes.

Installing react, react-dom, and react-scripts with cra-template...

npm ERR! Unexpected end of JSON input while parsing near '....1","object-hash":"^1'

npm ERR! A complete log of this run can be found in:

npm ERR! C:\Users\David\AppData\Roaming\npm-cache_logs\2020-02-12T03_53_37_836Z-debug.log

Aborting installation.

npm install --save --save-exact --loglevel error react react-dom react-scripts cra-template has failed.

I tried uninstalling and reinstalling node and also trying to install different older versions of reactjs but still got the same error. Also, I have tried clearing the node cache and then verifying it again via npm cache clear --force and then npm cache verify but still have not had any luck. Any help will be greatly appreciated.

The more detailed log shows this at the bottom:

203 silly saveTree `-- react@16.12.0

204 verbose stack SyntaxError: Unexpected end of JSON input while parsing near '....1","object-hash":"^1'

204 verbose stack at JSON.parse ()

204 verbose stack at parseJson (C:\Program Files\nodejs\node_modules\npm\node_modules\json-parse-better-errors\index.js:7:17)

204 verbose stack at C:\Program Files\nodejs\node_modules\npm\node_modules\node-fetch-npm\src\body.js:96:50

204 verbose stack at processTicksAndRejections (internal/process/task_queues.js:97:5)

205 verbose cwd C:\Users\David\Desktop\React\my-app

206 verbose Windows_NT 10.0.17763

207 verbose argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "install" "--save" "--save-exact" "--loglevel" "error" "react" "react-dom" "react-scripts" "cra-template"

208 verbose node v13.8.0

209 verbose npm v6.13.6

210 error Unexpected end of JSON input while parsing near '....1","object-hash":"^1'

211 verbose exit [ 1, true ]

David
  • 186
  • 1
  • 1
  • 9

7 Answers7

32

Try running this in your terminal:

npm cache clean --force

And redo the thing you are doing

Compiler v2
  • 3,509
  • 10
  • 31
  • 55
vijay kumar
  • 421
  • 5
  • 6
10

Installing yarn using node package manager fixed the issue for me. Open command prompt and enter:

npm install -g yarn
David
  • 186
  • 1
  • 1
  • 9
5

Solution:

npm cache clean --force

then try again to create your app (here when creating a react app) or install what you were about to install.

create-react-app myproject

(creating react app)[same npm problem that can occur in different operation]

npm install -g @angular/cli@latest

(installing angular cli (or installing anything else))

It will work.

explanation:

That's a problem related to npm, it's about the cache that get corrupt. Even though in newer versions of npm they implemented self healing, which normally guarantee no corruption, but it seem it's not that efficient. enter image description here Forcing clean cache resolve the problem.

The error happen when parsing one of the cache files, which have a json format. The cache is found at ~/.npm/_cacache (in linux) and %AppData%/npm-cache (windows). For my current npm version and when i checked, there was three directories.
enter image description here

if you check the first or the second, the structure is as follow enter image description here

And each cache file have a json format (and that what get parsed) enter image description here

Here a good link from the doc: https://docs.npmjs.com/cli/cache

[Update] Also if it happen that this didn't solve it, you may check this answer here https://stackoverflow.com/a/50191315/7668448 it show how you can change the npm registry, that can be helpful. Check and see.

Mohamed Allal
  • 17,920
  • 5
  • 94
  • 97
0

I had the same problem but npm cache clean --force didn't work for me.

What I did was CLEAN MY PC

using my cleaner(CCleaner) and npm install work now. I did it because the problem appears after I uninstall my node.js because I want to install nvm-windows on my PC (https://learn.microsoft.com/en-us/windows/nodejs/setup-on-windows), but after I did it I think some files and folders was leave behind making a conflict after I install node using nvm thus npm install do not work. So I thought cleaning my pc would solve the problem and it did.

These are just my thoughts. Hope to help someone.

Russel Santos
  • 27
  • 1
  • 1
  • 10
0

Problem solved using:

npm cache clean --force :/

Sambulo Senda
  • 1,388
  • 1
  • 14
  • 19
0

sudo npm install -g create-react-app worked for me

Indika Rajapaksha
  • 1,056
  • 1
  • 14
  • 36
0

It occurs due to hard caching in Yarn. I resolved the issue by deleting the node_modules folder and ran again

yarn install 

which generated a new node_modules folder and the issue was resolved.

sid7747
  • 690
  • 7
  • 15