Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected end of JSON input #600

Closed
suyogn opened this issue Feb 12, 2021 · 12 comments · Fixed by #645
Closed

Unexpected end of JSON input #600

suyogn opened this issue Feb 12, 2021 · 12 comments · Fixed by #645

Comments

@suyogn
Copy link

suyogn commented Feb 12, 2021

getting an error as Unexpected end of JSON input

@sindresorhus
Copy link
Owner

There's not enough info here to help you.

@dopecodez
Copy link
Collaborator

We'll need your terminal output and your system settings at least to take a look at this, @suyogn

@dopecodez
Copy link
Collaborator

Closing this for lack of information, @suyogn feel free to open this again if you feel like you need more help.

@hgascoigne
Copy link

@suyogn I ran into this same issue today. It was caused by me trying to publish a pre-release version of a package that did not yet exist in the registry. Publishing to a non-pre release version fixed this for me. np tries to JSON.parse a list of dist tags from npm, but since there aren't any yet npm throws an error.

@dopecodez
Copy link
Collaborator

Ah that's interesting @hgascoigne . We should not throw an error like unexpected JSON in that case, but actually throw an error saying there are no tags to parse from the npm registry.

I think this is a simple fix. A good PR would be welcome for the same.

@dopecodez
Copy link
Collaborator

So, I played around with reproducing this bug and was not able to.

When i tried to publish a pre-release of an unpublished package on npm, np ran without any complications.

Also, the function we use to parse the npm tags has methods to handle errors, and returns next in case there are no tags to parse from the npm registry.

@hgascoigne , any more pointers on how to reproduce this would be helpful.

@hgascoigne
Copy link

Sure, happy to provide some more details.

Reproduction:

  1. Create new empty repo with a single file, package.json.
 {
   "name": "hayden-test-repo",
   "version": "1.0.0",
   "description": "",
   "main": "index.js",
   "scripts": {
     "test": ""
   },
   "author": "",
   "license": "ISC"
 }
  1. Run np. Here's a screenshot of my selected options:
    image

Error
Inspecting np, execa throws the following error because the npm view command fails. np then runs into issues because error.stdout is an empty string so JSON.parse on this line fails.

{
    "shortMessage": "Command failed with exit code 1: npm view --json hayden-test-repo dist-tags",
    "command": "npm view --json hayden-test-repo dist-tags",
    "exitCode": 1,
    "stdout": "",
    "stderr": "npm ERR! code E404\nnpm ERR! 404 Not Found - GET https://registry.npmjs.org/hayden-test-repo - Not found\nnpm ERR! 404 \nnpm ERR! 404  'hayden-test-repo@latest' is not in the npm registry.\nnpm ERR! 404 You should bug the author to publish it (or use the name yourself!)\nnpm ERR! 404 \nnpm ERR! 404 Note that you can also install from a\nnpm ERR! 404 tarball, folder, http url, or git url.\n{\n  \"error\": {\n    \"code\": \"E404\",\n    \"summary\": \"Not Found - GET https://registry.npmjs.org/hayden-test-repo - Not found\",\n    \"detail\": \"\\n 'hayden-test-repo@latest' is not in the npm registry.\\nYou should bug the author to publish it (or use the name yourself!)\\n\\nNote that you can also install from a\\ntarball, folder, http url, or git url.\"\n  }\n}\n\nnpm ERR! A complete log of this run can be found in:\nnpm ERR!     /Users/hayden/.npm/_logs/2021-04-12T14_41_53_367Z-debug.log",
    "failed": true,
    "timedOut": false,
    "isCanceled": false,
    "killed": false
}

Solution
I see the JSON formatted error that I think np is looking for but it lives in error.stderr and is wrapped with plain text so simply changing the line to JSON.parse(error.stderr) wouldn't work either. Not sure if this NPM bug is related. I'm using npm@7.7.4 and it looks like they changed where errors get output to.

@sebacruz
Copy link

When i tried to publish a pre-release of an unpublished package on npm, np ran without any complications.

I'm trying to publish a pre-release of an unpublished package and I get the Unexpected end of JSON input error from np.

What information can I provide to help solve this problem?

@dopecodez
Copy link
Collaborator

@sebacruz , Currently, we are blocked by this issue in npm. Only possible solution would be to create a initial release before doing pre-releases.

The only other way is to rollback your npm version to ~ npm@3.x.x where this error didn't exist in the npm cli.

@searleser97
Copy link

Same issue here

@Romanchuk
Copy link

add .npmignore file on your package.json level

# Node generated files
node_modules
npm-debug.log
# OS generated files
Thumbs.db
.DS_Store
# Ignored files
*.ts
!*.d.ts
# IDE files
.vscode
.json
# Source files
src

It made it work!

@jiricerhan
Copy link

Switching to a newer version of node helped (from 14.15.4 to 16.18.0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants