user6124024
user6124024

Reputation:

NPM install error "Unexpected token < in JSON at position 0"

I'm using npm install and validate the package JSON content is valid and I'm getting the following error:

After reading on the web, I did "rm -f package-lock.json && npm install" (even if I didn’t see any package lock there) and did also npm cache clean -f which doesn’t help, also configured registry.

See Can't install any package with node npm

I'm using the latest npm, 6.2.0.

The error in the logs is:

159 silly saveTree ├── [email protected]
159 silly saveTree ├── [email protected]
159 silly saveTree └── [email protected]
160 verbose stack SyntaxError: Unexpected token < in JSON at position 0 while parsing near '<html>
160 verbose stack <head><title>...'
160 verbose stack     at JSON.parse (<anonymous>)
160 verbose stack     at parseJson (/usr/local/lib/node_modules/npm/node_modules/json-parse-better-errors/index.js:7:17)
160 verbose stack     at consumeBody.call.then.buffer (/usr/local/lib/node_modules/npm/node_modules/node-fetch-npm/src/body.js:96:50)
160 verbose stack     at <anonymous>
160 verbose stack     at process._tickCallback (internal/process/next_tick.js:188:7)

Is there is something else I can do?

Upvotes: 13

Views: 39471

Answers (8)

Ivar Johansson
Ivar Johansson

Reputation: 1

I had a similar issue on one of our repos. In our case the package.json file was encoded as UTF-8-BOM instead of UTF-8, and the JSON data in the file could not be parsed.

Upvotes: 0

笑先生
笑先生

Reputation: 57

I came across this issue, as well.

This solution in Can't install any package with node npm helps me.

The command is below:

npm install <packagename> --registry http://registry.npmjs.org/

But, this is painful. After a while, I found that I set the wrong registry mirror.

  • Firstly, I use the command below to find out my registry
    npm config ls -l 
    
    Then I found the value is http://www.npmjs.org/
  • Secondly, I use the command below to fix it
    npm config set registry https://registry.npmjs.org/
    

After that, I solved it!

Upvotes: 0

Aditya Gaddhyan
Aditya Gaddhyan

Reputation: 364

I got the same error in NestJS and I found out that the error was in nest-cli.json and not in any other file.

Upvotes: 0

Saurish Kar
Saurish Kar

Reputation: 766

For people who have removed node_modules and facing error after npm install. This can also be a problem if you have a preinstall script under package.json like this:

"scripts": {
  "preinstall": "npx npm-force-resolutions",
....

Since npm-force-resolutions looks for node_modules to resolve. To fix it, just comment the preinstall script, run npm install and then revert the comment and run npm install again just to keep things verified.

Upvotes: 0

Aathil Ahamed
Aathil Ahamed

Reputation: 474

Deleting Node Modules and package-lock JSON file, solves this issue in many cases.

These are the steps to be followed:

  1. Delete the node_modules folder.

  2. Delete package-lock.json file.

  3. Verify the cache

  4. Run npm install again.

Let's use rimraf

  1. Install rimraf globally

    npm install rimraf -g

  2. Remove node modules

    rm -rf node_modules

  3. Remove package-lock.json

    rimraf package-lock.json

  4. Verify cache

    npm cache verify

  5. Install the modules again

    npm install

Upvotes: 0

Kamil Naja
Kamil Naja

Reputation: 6692

Your package.json, or maybe other JSON file is incorrect. You must first fix JSON errors.

Upvotes: 4

Latin Warrior
Latin Warrior

Reputation: 932

This worked for me:

rm -rf node_modules
rm package-lock.json
npm cache verify
npm install

Upvotes: 4

shamon shamsudeen
shamon shamsudeen

Reputation: 5848

Do the following:

  1. Delete the node_modules folder.

  2. Delete package-lock.json file.

  3. Run npm install again.

Upvotes: 12

Related Questions