Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add filename to require() json errors

otherwise it can be quite difficult to figure
out which file is busted, in our case during a build phase
  • Loading branch information...
commit d2912c5be6fcb0baae215b0c7b018301469ea1aa 1 parent 2d0011f
@tj authored
View
7 lib/module.js
@@ -471,7 +471,12 @@ Module._extensions['.js'] = function(module, filename) {
// Native extension for .json
Module._extensions['.json'] = function(module, filename) {
var content = NativeModule.require('fs').readFileSync(filename, 'utf8');
- module.exports = JSON.parse(stripBOM(content));
+ try {
+ module.exports = JSON.parse(stripBOM(content));
+ } catch (err) {
+ err.message = filename + ': ' + err.message;
+ throw err;
+ }
};
View
5 test/fixtures/invalid.json
@@ -0,0 +1,5 @@
+{
+ "name": "foo",
+ "version": "0.0.1"
+ "description": "im broken"
+}
View
9 test/simple/test-require-json.js
@@ -0,0 +1,9 @@
+
+var assert = require('assert');
+
+try {
+ require('../fixtures/invalid.json');
+} catch (err) {
+ var i = err.message.indexOf('test/fixtures/invalid.json: Unexpected string')
+ assert(-1 != i, 'require() json error should include path');
+}
Please sign in to comment.
Something went wrong with that request. Please try again.