Support for .json extension on require() #584

Closed
wants to merge 1 commit into
from

Projects

None yet

3 participants

@aikar
aikar commented Jan 26, 2011

Added support for loading .json files through require, where the contents of said required file is simply a JS value (string, bool, float, int, array, object)

equiv to module.exports = { foo: 'bar'; } but cleaner and name files .json.

Test cases included and ensures that .js still has priority over .json

@aikar aikar Added support for loading .json files through require, where the cont…
…ents of said required file is simply a JS value (string, bool, float, int, array, object)

equiv to module.exports = { foo: 'bar'; } but cleaner and name files .json.

Test cases included and ensures that .js still has priority over .json
ee49867
@ThisIsMissEm

What's wrong with:

 var myFileInJson = JSON.parse(fs.readFileSync(myfile))

?

@ry
ry commented Jan 26, 2011

i dont think this is needed

@aikar
aikar commented Jan 26, 2011

Well to answer whats wrong for miksago, This implementation would actually be loading a JS Object and not JSON, meaning you could do typical JS syntax and features.

For example you could have the following:
{
isDev: process.env['ENVIRONMENT'] && process.env['ENVIRONMENT'] == 'dev' ? true: false,
siteUrl: 'blah'
}

Where as that kind of stuff would not be valid in JSON. To implement this ability in userland would require running it through the VM, and also would not provide the cache features node provides to modules.

The main reason for this is currently I (and im sure others) may store config files in .js files to be
required(), and currently have to do it in the format module.exports = {foo: bar};

but I guess this would be better to do in userland and just make the wrapper yourself.

So I retract this pull request also due to it not actually being JSON.

@ThisIsMissEm

wrong. See that the file was sent through JSON.parse, which would die if you have invalid json.

@aikar
aikar commented Jan 26, 2011

I know, What I meant was that was WHY I submitted my change, because you COULD do JavaScript things which is not valid JSON.

But thats also why im closing it since its not JSON.

I thought about the idea of changing the extension to be .jsc for "JS Config" but theres no need for this specific change since its not really json and can be confusing.

But as ry said, theres no need for this as it can be implemented rather trivially in userland.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment