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

Already on GitHub? Sign in to your account

have npm report the error with a package.json #3869

Pomax opened this Issue Sep 6, 2013 · 8 comments


None yet
5 participants

Pomax commented Sep 6, 2013

since npm relies on a well formatted package.json file, it would be nice if it could report the actual error it sees when trying to parse a bad package.json rather than merely reporting that there is some error without saying which line/column or the type of error encountered. If there is a JSON parse error perhaps the package.json file could be thrown at jshint to get the precise error?


domenic commented Sep 6, 2013

I don't believe any JSON parsers expose this information, and we just use JSON.parse, which definitely doesn't. JSHint is not a JSON parser.

Pomax commented Sep 6, 2013

I know, I'm not saying to use jshint instead of JSON parsing, but that it would be very helpful if, after a JSON.parse throws its parse error, that error is handled by running the package.json file that we now know has errors through a JS linter, which unlike JSON.parse will find the error tied to a location in the file and an error case number, which can then be reported as additional information. e.g.:

try {
  var package = require("./package.json");
catch (e) { 
  // alright, error in package.json. But what's the error?
  require("somelintinglibrary").lint("./package.json", function(err, result) {
    // we already know it's going to error out
    console.log("error in package.json: " + err.message + " (" + err.code + ")");

or some such. Once you know that package.json is guaranteed to have an error it can be run through a linter to find the exact location and nature of the error, so that the user can find out what's wrong much faster than they currently can.


domenic commented Sep 6, 2013

Yeah, I got that. But do you know of any JSON parsers that include line numbers in their error messages? JSHint isn't a JSON parser, so that won't work. (E.g. if your syntax error is forgetting quotes, JSHint will be fine with that.)


domenic commented Sep 6, 2013

Could work. Would be helpful to run some tests on different invalid inputs and see what it gives you.

@rlidwka rlidwka added a commit to rlidwka/npm that referenced this issue Dec 24, 2013

@rlidwka rlidwka make JSON.parse return error positions
fix for #3869

adius commented Apr 24, 2014


@othiym23 othiym23 removed the nice to have label Oct 7, 2014

Any progress on this? Seems like something so simple but so important when all that is usually missing is a comma or something visually insignificant.


othiym23 commented Mar 6, 2015

Yes! There has been! @smikes has done is all a huge favor!

Closed by abdd040. This is included in npm@2.7.1, which is the version currently being tested (npm@next).

@othiym23 othiym23 closed this Mar 6, 2015

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