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

8

13

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?

Weston answered 16/7, 2018 at 9:31 Comment(1)
if using Visual Studio Community edition 2017 check that you are in the project folder in the Package Manager Console and not in the solution folder, this caught me out!Curt
G
4

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

Godber answered 16/7, 2018 at 10:2 Comment(1)
Please try remove your node_modules and reinstallGodber
L
12

Do the following:

  1. Delete the node_modules folder.

  2. Delete package-lock.json file.

  3. Run npm install again.

Lindbergh answered 16/7, 2018 at 12:17 Comment(0)
G
4

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

Godber answered 16/7, 2018 at 10:2 Comment(1)
Please try remove your node_modules and reinstallGodber
C
4

This worked for me:

rm -rf node_modules
rm package-lock.json
npm cache verify
npm install
Caspian answered 23/9, 2019 at 15:11 Comment(0)
M
0

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

Mcguire answered 7/10, 2020 at 14:12 Comment(0)
C
0

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.

Claresta answered 24/3, 2021 at 5:59 Comment(0)
E
0

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.

Erna answered 7/4, 2022 at 6:41 Comment(0)
P
0

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!

Pryor answered 28/7, 2023 at 12:22 Comment(0)
G
0

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.

Giorgia answered 25/4 at 15:16 Comment(1)
Welcome to stackoverflow and thank you for contributing! I edited a bit your answer. If you have additional details to solve the problem (eg. how to convert from UTF-8-BOM to UTF-8) don't hesitate to add them.Lenee

© 2022 - 2024 — McMap. All rights reserved.