Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
Pomax opened this Issue · 8 comments

5 participants

@Pomax

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
Collaborator

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

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
Collaborator

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
Collaborator

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

@rlidwka rlidwka referenced this issue from a commit in rlidwka/npm
@rlidwka rlidwka make JSON.parse return error positions
fix for #3869
ab80535
@adius

+1

@othiym23 othiym23 removed the nice to have label
@ilanbiala

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
Owner

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.