mozilla/node-convict

Support using convict in a (browserify-based) browser context

ronjouch opened this issue · 4 comments

Hi.

I'm trying to use your package in a browserify setup, but because conf.loadFile('some.json') calls various fs methods (fs.statSync, fs.readdirSync), it fails in the browser.

→ Is it something that sounds feasible with convict (or maybe with a browserify transform?), or do you suggest going for another package / custom solution?

Thanks for the help, and sorry for posting this as an issue, it's more of a question to which "we do not and will not support that" can be a fine answer.

@ronjouch convict in itself does not require fs operations, and anyway those are ignored by default by browserify, cf. https://github.com/substack/browserify-handbook#ignoring-and-excluding : "browserify already 'ignores' the 'fs' module by returning an empty object".

Nevertheless convict has dependencies over cjson to load filesystem files. And cjson, in turn, requires the path module. So ignoring the path module with browserify may make it possible to use convict in browser.

@madarche thanks for the response! Yes that's part of the path I followed and I'm now using convict for an application at work. In case that helps someone, the path I followed was:

  • Ignore the system and file modules (in Gulp, adding two .ignore('system').ignore('file') calls).
  • Use brfs to ensure the fs.loadFileSync schema-loading calls are inlined at build time rather than done at runtime.
  • Add a level of indirection using superagent to support loading configuration "from a .json URL". The XHR callback just parses the response and calls your .load() method. I considered proposing a patch to your library, but the changes were getting invasive and I doubt you'd have merged them.

@ronjouch thanks for the answer. I agree with you that we should not widen convict scope and add new functionalities, we should follow the Node aesthetic.

Nevertheless:

  • if using convict in a browser context proves to be useful in your experience, a PR for documenting in the README.md how to do it would be worthwhile
  • if you think that convict could be improved to more easily "plug" other modules on it, please explain how, PR welcomed

Cheers

There are a few things preventing this from being browserified:

  • dep uses v8-specific Error functions. Works on Chrome. Doesn't anywhere else.
  • convict.load now uses fstat, which isn't implemented by brfs

Either drop the browserify support or fix these issues. Browserify-ability requires a lot of work and greatly limits your choices, so I also suggest adding some karma/phantomjs-based tests so you can catch issues early.