From 588d885e81dec667920383ac7246daceeb7f99fd Mon Sep 17 00:00:00 2001 From: isaacs Date: Wed, 20 Jul 2011 17:39:23 -0700 Subject: [PATCH] Close #1357 Load json files with require() Signed off by everybody. --- lib/module.js | 7 +++++++ test/simple/test-module-loading.js | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/lib/module.js b/lib/module.js index 1c3d4d3acf..6f32620e2c 100644 --- a/lib/module.js +++ b/lib/module.js @@ -466,6 +466,13 @@ Module._extensions['.node'] = 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(content); +}; + + // bootstrap main module. Module.runMain = function() { // Load the main module--the command line argument. diff --git a/test/simple/test-module-loading.js b/test/simple/test-module-loading.js index 736a1e2e49..4d683ebf27 100644 --- a/test/simple/test-module-loading.js +++ b/test/simple/test-module-loading.js @@ -213,6 +213,13 @@ var child = require('../fixtures/module-require/child/'); assert.equal(child.loaded, parent.loaded); +// #1357 Loading JSON files with require() +var json = require('../fixtures/packages/main/package.json'); +assert.deepEqual(json, { name: 'package-name', + version: '1.2.3', + main: 'package-main-module' }); + + process.addListener('exit', function() { assert.ok(common.indirectInstanceOf(a.A, Function)); assert.equal('A done', a.A());