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 #42

Open
jrop opened this issue Jun 2, 2017 · 7 comments
Open

Unexpected end of JSON input #42

jrop opened this issue Jun 2, 2017 · 7 comments

Comments

@jrop
Copy link

jrop commented Jun 2, 2017

First of all I love this utility, thanks for making!

I am getting the following error:

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at getDependencyTree (/Users/me/.config/yarn/global/node_modules/cost-of-modules/lib/helpers.js:64:17)
    at Object.getRootDependencies (/Users/me/.config/yarn/global/node_modules/cost-of-modules/lib/helpers.js:73:26)
    at Object.<anonymous> (/Users/me/.config/yarn/global/node_modules/cost-of-modules/lib/index.js:26:32)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)

My package.json is:

{
  "name": "gtest",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "devDependencies": {
    "gulp": "gulpjs/gulp#4.0",
    "gulp-cli": "^1.3.0"
  }
}

I am using npm 5.0.0, and I suspect that the issue relates to npm ls --json printing errors after printing the json (note that sync-exec channels both STDOUT and STDERR and returns the combination).

You should probably use:

const {spawnSync} = require('child_process')
const proc = spawnSync('npm', ['ls', '--json'])
console.log(JSON.parse(proc.stdout.toString()))
// ignore proc.stderr

Erroneous "JSON" output (note STDERR at end):

{
  "name": "gtest",
  "version": "1.0.0",
  "problems": [
    "invalid: [email protected] /Users/me/Desktop/gtest/node_modules/gulp",
    "extraneous: [email protected] /Users/me/Desktop/gtest/node_modules/glob-watcher",
    "extraneous: [email protected] /Users/me/Desktop/gtest/node_modules/undertaker",
    "extraneous: [email protected] /Users/me/Desktop/gtest/node_modules/vinyl-fs",
    "extraneous: [email protected] /Users/me/Desktop/gtest/node_modules/node-pre-gyp"
  ],
  "dependencies": {
    "...": "..."
  }
}
npm ERR! invalid: [email protected] /Users/me/Desktop/gtest/node_modules/gulp
npm ERR! extraneous: [email protected] /Users/me/Desktop/gtest/node_modules/glob-watcher
npm ERR! extraneous: [email protected] /Users/me/Desktop/gtest/node_modules/undertaker
npm ERR! extraneous: [email protected] /Users/me/Desktop/gtest/node_modules/vinyl-fs
npm ERR! extraneous: [email protected] /Users/me/Desktop/gtest/node_modules/node-pre-gyp
@siddharthkp
Copy link
Owner

siddharthkp commented Jun 2, 2017

Thanks, that's super useful!

I'll happily take a PR if you would like to submit

@jrop
Copy link
Author

jrop commented Jun 2, 2017

This is turning out to be more difficult than I first thought. In my particular case the npm process is exiting before flushing STDOUT, which means that NodeJS gets truncated output back from it. I haven't figured out a workaround for this yet. I am investigating seeing if it is possible to switch to read-package-tree to see if that gives all of the data your utility needs, but the problem with that is that is an asynchronous module, and your code is synchronous. Lots to investigate

@jrop
Copy link
Author

jrop commented Jun 2, 2017

This seems to be a bug with NPM.

According to NodeJS (nodejs/node/issues/9633) all of STDIO is async, and if the process exits before all the data is flushed, it will not be able to be ready from child_process.

In NPM's lib/utils/error-handler.js file, in exit(code, noLog) -> reallyExit(err) the process is exited explicitly with process.exit(code). If I change that line to process.exitCode = code, then I am able to read all of the child_process's STDOUT.

@denieler
Copy link

the same issue with npm 5.1.0

@tvvignesh
Copy link

Same issue on my end. Works well in one project, but throws unexpected end of json in another. Screenshot below.

Screenshot from 2020-03-29 23-48-34

@michaelfarrell76
Copy link

Screen Shot 2021-02-06 at 9 30 24 PM

@JLarky
Copy link

JLarky commented Jan 27, 2023

I have the same issue with yarn as well (yarn list --json)

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

No branches or pull requests

6 participants