kovogel
kovogel

Reputation: 1030

npm "failed to parse json"

When I'm trying to install express with npm I always get the following error:

Failed to parse json
No data, empty input at 1:1
File: /root/.npm/inherits/2.0.1/package/package.json
Failed to parse package.json data.
package.json must be actual JSON, not just JavaScript.

This is not a bug in npm.
Tell the package author to fix their package.json file. JSON.parse

What am I doing wrong?

 sudo npm install -g express

OS is Ubuntu 12.04 (precise) armhf

Upvotes: 46

Views: 114367

Answers (20)

GGWilliams
GGWilliams

Reputation: 31

You could get this error from not doing npm init.

Upvotes: 3

Bilal Hussain
Bilal Hussain

Reputation: 572

For me the issue was fixed by changing the name of the package from

"name": "portfolio"

to

"name": "portfolio2"

Upvotes: 1

Saimohan Charugundla
Saimohan Charugundla

Reputation: 13

1.Basically it comes for wrong placement of comma so remove comma at wrong position(esp error occurs for placing comma(,) before closing flower brace('}') in package.json so look into it. This is one solution

  1. Run

sudo npm cache clean

sudo chown -R 1000:1000 "...path/.npm"

Upvotes: 0

Aman Singh
Aman Singh

Reputation: 385

remove any unnecessary comments, the error you referred occurs usually due to the syntax error. Or if this won't help, try to clean the cache by "npm cache clean".

Upvotes: 0

Jexon
Jexon

Reputation: 53

For those of you who are new to this like me, I forgot to initialize my JSON package with the npm init command.

Upvotes: 0

Prashant Yalatwar
Prashant Yalatwar

Reputation: 179

I solved the issue using below steps:

  1. Delete node_modules folder

  2. Delete package-lock.json file

  3. Run npm install

  4. Run npm start

Upvotes: 4

MD SHAYON
MD SHAYON

Reputation: 8063

I think you may be did some change in package.json and that is not valid

  1. Delete node_modules
  2. Delete package.json
  3. Create a new NPM package with

npm init

  1. install all of your package once again

npm install express

Upvotes: 0

Damon Wu
Damon Wu

Reputation: 57

Please check with unused white spaces inside package.json file, it might cause by extra white spaces.

Upvotes: 0

Harry_C
Harry_C

Reputation: 177

I faced this problem several times before I got used to using NPM. Most off the time it was because I failed to use npm init before npm install

Upvotes: 1

abmap
abmap

Reputation: 309

In addition to Pank's answer, if you encounter this kind of error

npm ERR! code EJSONPARSE
npm ERR! JSON.parse Failed to parse json
npm ERR! JSON.parse Unexpected token } in JSON at position 550 while parsing near '...eact": "^7.12.4",
npm ERR! JSON.parse   },
npm ERR! JSON.parse   "dependencies":...'
npm ERR! JSON.parse Failed to parse package.json data.
npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.

You need to make sure your package.json is a valid json, not a javascript.

Upvotes: 0

Dere Sagar
Dere Sagar

Reputation: 1818

In my case Missing a comma somewhere in a package.json Check your package.json file. After that sudo npm install

or

To clean the cache memory. sudo npm cache clean

Upvotes: 0

Tomer Bar-Shlomo
Tomer Bar-Shlomo

Reputation: 441

The following bash script fixes the problem automatically

#!/usr/bin/env bash
echo -e '#!/usr/bin/env bash' > npm_install.sh
cat npm-debug.log | grep 'error File:' | sed -n 's:.*error File\: \(.*\):echo "Removing \1"\nrm -f \1\necho "Cleaning npm cache"\nnpm cache clean\necho "Reinstalling npm"\nnpm install\n./npm_reinstall.sh:p' >> npm_install.sh
chmod +x npm_install.sh
./npm_install.sh

It should be saved to npm_reinstall.sh and granted execution permissions using

chmod +x npm_reinstall.sh

The script is preforming the following tasks:

  1. Looking for an error File : in npm-debug.log using grep
  2. Using sed to generate the fix commands 3-5 only if there are errors
  3. Removing the empty file rm -f /1 = file path from the first group in regular expression .*error File: (.*)
  4. Cleaning npm cache npm cache clean
  5. Reinstalling npm npm install
  6. Running ./npm_reinstall.sh recursively till no errors are found

More information about npm install can be found at npm-install command documentation

Upvotes: 0

cgarvey
cgarvey

Reputation: 221

I experienced a similar issue today after updating Node on Windows 10. My local build tasks started failing and upon investigation I saw all these errors in my dependency package.json files. None of them were valid JSON anymore and I was seeing messages like:

npm WARN Failed to parse json
npm WARN Unexpected token '\u0019' at 1:1
npm WARN ������2�����bE�;���1L �\5�e���k2?��,?;��쏏a��(T��w��+I��/�6�P} ��i�|e�
npm WARN ^

in my console.

This story has a happy ending as it turns out that new Node doesn't play nice with old NPM and updating NPM to version 5 solved the problem. Hope this helps other folks who may experience this variation on this issue.

Upvotes: 0

kovogel
kovogel

Reputation: 1030

Thanks to Jivings from this comment:

npm cache clean

solved the problem.

Upvotes: 103

Ashish Mishra
Ashish Mishra

Reputation: 93

Mostly, this error is due to a syntax error in package.json file. In my case, the opening curly brace for dependencies object in package.json was missing:-

Code--------------------------------

{
  "name": "psrxjs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": 
    "rxjs": "^5.4.3"
  }
}

Upvotes: 5

adeguk Loggcity
adeguk Loggcity

Reputation: 372

I had the same problem but "npm cache clean" didnt resolve it for me. I had to go back to my package.json and realize i had comma where it shouldn't i suppose as shown below:

},
  "devDependencies": {
    "axios": "^0.15.3",
    "bootstrap-sass": "^3.3.7",
    "cross-env": "^3.2.4",
    "jquery": "^3.1.1",
    "laravel-mix": "0.*",
    "lodash": "^4.17.4",
    "vue": "^2.1.10",
  }

after the "vue..." so I deleted that and every went back to normal. So it is worth checking the package.json file first before running npm cache clean

Upvotes: 14

Karo
Karo

Reputation: 1

Try to open your txt editor and select "plain text" for the package.json then re-save. Sometimes issue is overlooked and simple things are holding the answer.

Upvotes: -1

B Ii
B Ii

Reputation: 49

In Laravel project:

  1. Delete 'node_modules' folder;
  2. npm cache clean
  3. npm update

Upvotes: 4

Avijit Majhi
Avijit Majhi

Reputation: 518

I also got the same error message while run npm install, first run npm package.json to check errors in package.json file, if not then run npm cache clean

Upvotes: 2

Don Wayne
Don Wayne

Reputation: 11

Don't forget to edit your package.json, escpeially the dependencies.

For example, one of my chatting room projects needs the following content in package.json:

{
    "name":"chatrooms",
    "version":"0.0.1",
    "description":"Minimalist multi-room chat server",
    "dependencies":{
        "socket.io":"~0.9.6",
        "mime":"~1.2.7"
    }
}

Upvotes: -1

Related Questions