Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

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

Closed
Pomax opened this issue Sep 6, 2013 · 8 comments
Closed

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

Pomax opened this issue Sep 6, 2013 · 8 comments

Comments

@Pomax
Copy link

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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.)

@Pomax
Copy link
Author

Pomax commented Sep 6, 2013

Hmm, what about https://github.com/zaach/jsonlint ?

@domenic
Copy link
Contributor

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 added a commit to rlidwka/npm that referenced this issue Dec 24, 2013
@adius
Copy link

adius commented Apr 24, 2014

+1

@ilanbiala
Copy link

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
Copy link
Contributor

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 as completed Mar 6, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants