Raise an error when a malformed package.json file is found.
The current behaviour will silently ignore any parsing errors that may occur when loading a package.json file. This makes debugging errors in the package.json file very difficult. This changes the behaviour that that errors opening and reading the file package.json file continue to be ignored, but errors in parsing will throw an exception.
This commit is contained in:
parent
94bedc60f4
commit
a4e10cdb07
@ -103,11 +103,18 @@ function readPackage(requestPath) {
|
|||||||
try {
|
try {
|
||||||
var jsonPath = path.resolve(requestPath, 'package.json');
|
var jsonPath = path.resolve(requestPath, 'package.json');
|
||||||
var json = fs.readFileSync(jsonPath, 'utf8');
|
var json = fs.readFileSync(jsonPath, 'utf8');
|
||||||
var pkg = packageCache[requestPath] = JSON.parse(json);
|
} catch (e) {
|
||||||
return pkg;
|
|
||||||
} catch (e) {}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
var pkg = packageCache[requestPath] = JSON.parse(json);
|
||||||
|
} catch (e) {
|
||||||
|
e.path = jsonPath;
|
||||||
|
e.message = 'Error parsing ' + jsonPath + ': ' + e.message;
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
return pkg;
|
||||||
}
|
}
|
||||||
|
|
||||||
function tryPackage(requestPath, exts) {
|
function tryPackage(requestPath, exts) {
|
||||||
@ -123,7 +130,7 @@ function tryPackage(requestPath, exts) {
|
|||||||
// In order to minimize unnecessary lstat() calls,
|
// In order to minimize unnecessary lstat() calls,
|
||||||
// this cache is a list of known-real paths.
|
// this cache is a list of known-real paths.
|
||||||
// Set to an empty object to reset.
|
// Set to an empty object to reset.
|
||||||
Module._realpathCache = {}
|
Module._realpathCache = {};
|
||||||
|
|
||||||
// check if the file exists and is not a directory
|
// check if the file exists and is not a directory
|
||||||
function tryFile(requestPath) {
|
function tryFile(requestPath) {
|
||||||
@ -209,7 +216,7 @@ Module._nodeModulePaths = function(from) {
|
|||||||
var paths = [];
|
var paths = [];
|
||||||
var parts = from.split(splitRe);
|
var parts = from.split(splitRe);
|
||||||
|
|
||||||
for (var tip = parts.length - 1; tip >= 0; tip --) {
|
for (var tip = parts.length - 1; tip >= 0; tip--) {
|
||||||
// don't search in .../node_modules/node_modules
|
// don't search in .../node_modules/node_modules
|
||||||
if (parts[tip] === 'node_modules') continue;
|
if (parts[tip] === 'node_modules') continue;
|
||||||
var dir = parts.slice(0, tip + 1).concat('node_modules').join(joiner);
|
var dir = parts.slice(0, tip + 1).concat('node_modules').join(joiner);
|
||||||
@ -217,7 +224,7 @@ Module._nodeModulePaths = function(from) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return paths;
|
return paths;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
Module._resolveLookupPaths = function(request, parent) {
|
Module._resolveLookupPaths = function(request, parent) {
|
||||||
@ -367,7 +374,7 @@ Module.prototype._compile = function(content, filename) {
|
|||||||
|
|
||||||
Object.defineProperty(require, 'paths', { get: function() {
|
Object.defineProperty(require, 'paths', { get: function() {
|
||||||
throw new Error('require.paths is removed. Use ' +
|
throw new Error('require.paths is removed. Use ' +
|
||||||
'node_modules folders, or the NODE_PATH '+
|
'node_modules folders, or the NODE_PATH ' +
|
||||||
'environment variable instead.');
|
'environment variable instead.');
|
||||||
}});
|
}});
|
||||||
|
|
||||||
@ -445,7 +452,7 @@ Module._extensions['.js'] = function(module, filename) {
|
|||||||
|
|
||||||
|
|
||||||
// Native extension for .json
|
// Native extension for .json
|
||||||
Module._extensions['.json'] = function (module, filename) {
|
Module._extensions['.json'] = function(module, filename) {
|
||||||
var content = NativeModule.require('fs').readFileSync(filename, 'utf8');
|
var content = NativeModule.require('fs').readFileSync(filename, 'utf8');
|
||||||
module.exports = JSON.parse(stripBOM(content));
|
module.exports = JSON.parse(stripBOM(content));
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user