Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

require() json files #1357

Closed
isaacs opened this Issue · 34 comments
@isaacs
Owner

It'd be really handy to be able to require() a JSON file. With this:

require.extensions[".json"] = function (m) {
 m.exports = JSON.parse(fs.readFileSync(m.filename));
};

you could do:

var config = require("./config.json")
@dshaw

+1

@aseemk

+500!

@azproduction

I agree. JSON is the de facto standard for configuration or data files in Node.js. I always have to write the same wrapper.

@wavded

+1

@deanlandolt

This would be handy, sure. Even though I know it's probably overkill for node I figured I'd throw out the AMD plugins option. It'd also be useful for other kinds of require branching like i18n and other potentially useful resource parsers ("yaml!config.yaml"). Oh, and I think there was some interest in requiring from a zip archive -- all those kind of things could be done easily from userland.

But yeah, another level of misdirection sucks, so my guess is this is a non-starter -- just figured I'd chuck it out there again. I remember there being some interest in getting require.extensions to compose better -- this is one possible approach, but I have no idea how well this would interact with the current require.extensions stuff (my guess is poorly). But one way or another, I'd kill for a straitforward, composable way to hook into require.

@3rd-Eden

+100

@partheseas

+9001

@aseemk

There needs to be a "Like" or "+1" button for GitHub comments (or whole threads). <3

@goatslacker

+1!!!!

@Marak

FYI, we use nconf for this. Once you get the idea of using json based configuration files, having a friendly interface to your JSON tends to help.

https://github.com/indexzero/nconf

@indexzero

@isaacs Do you think it would be useful to try to make this operation "safer"?

require.extensions[".json"] = function (m) {
  try {
    m.exports = JSON.parse(fs.readFileSync(m.filename));
  }
  catch (ex) {
    m.exports = {};
  }
};

It seems like it would save a lot of users from having to constantly wrap their require() statements to safe-guard against potential parse errors.

+1 overall though

@tj
tj commented

+1 from me, it's small enough to be a "why not"

@gf3

@indexzero Good idea, although I'd be more inclined to make it null or false, that way one can differentiate between parse errors and empty files.

@stephank

Gosh, that's a great idea. I feel really dumb now for doing something similar but not nearly as neat using the vm module.

I had a libYAML binding lying around that I was working on a while ago. Couldn't resist putting this in and publishing to npm (as package libyaml).

The bindings parse a good deal of YAML, and should be able to deal with most anything using it for configuration. Some fancy features (anchors and whatnot) are unimplemented, and serializing is very incomplete.

@tj
tj commented

for .json I think you'd want to know of the parse errors, could be potentially confusing otherwise

@josh

@indexzero Getting a parse error seems consistent with requiring invalid JS. You don't get an empty object back if there is a JS parse error.

Related #584

@isaacs
Owner
@isaacs
Owner

@deanlandolt Yeah, that stuff is a much bigger discussion. I'm of the opinion that stuff like archive require()ing and adding syntax should not be in node-core. JSON parsing is built into the language already, so it's pretty easy.

@isaacs isaacs closed this
@isaacs isaacs reopened this
@gasi

+1

@shinuza

+42

@ricardobeat

nice. +1 for not catching errors.

@felixge
Owner

+1

@ry
ry commented

+1

@isaacs isaacs closed this issue from a commit
@isaacs isaacs Close #1357 Load json files with require()
Signed off by everybody.
588d885
@isaacs isaacs closed this in 588d885
@thurmda

I like this idea but I've never felt much pain in loading my configs like this:

var config = require('config')

where config.js is:
module.exports = config = { ... }

Check out this commit:
thurmda/hummingbird@fdb97bb

Technically my config is a module that exports and object literal, which I find a bit nicer than writing strict json ( " vs ' etc.)

@dresende

A good improvement might be to add a default object when then file is not found.

@felixge
Owner

A good improvement might be to add a default object when then file is not found.

-1, this would bring inconsistencies to the way require works, when a file is not found, it should throw in all cases.

@dresende

Maybe. I try to avoid exceptions. I would prefer to have null returned or a default config object if set as a second parameter, because that's what I would do next. I'll probably have to wrap require() around my own method.

@goatslacker

If you scroll up the thread you'll notice that @josh explained why this is not the best implementation.

@armw4

Even all this time later I'd like to just say :+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.