Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Support for .json extension on require() #584

Closed
wants to merge 1 commit into from
Closed

Support for .json extension on require() #584

wants to merge 1 commit into from

Conversation

aikar
Copy link

@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

…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
@ThisIsMissEm
Copy link

What's wrong with:

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

?

@ry
Copy link

ry commented Jan 26, 2011

i dont think this is needed

@aikar
Copy link
Author

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

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

@aikar
Copy link
Author

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 pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants