$ node run.js
[Sat May 11 2013 19:13:06 GMT-0400 (EDT)] ERROR { [Error: Cannot find module './app.yaml'] code: 'MODULE_NOT_FOUND' }
env = development
{}
env = development
{ redis: { port: 6379, host: '127.0.0.1', password: '', db: 1, options: {} } }
[ '.npmignore',
'app.yaml',
'config',
'example',
'freq.js',
'node_modules',
'run.js',
'test.js',
'whereami.js' ]
Output of node.js code below
var fs = require('fs')
fs.readdir('.', function(err, files) {
console.log(files);
});
var config = require('yaml-config');
var env = 'development';
var settings = config.readConfig('./app.yaml');
// path from your app root without slash
console.log('env = %s', env);
console.log(settings);
settings = config.readConfig('/Users/shawn/dev/node.js/example/app.yaml');
console.log('env = %s', env);
console.log(settings);
Brandon, fair point. I was using the example from the npm page. I've simplified the code and combined my file system check.
I've had issues in the past with modules that dynamically 'load' config files using require() and a relative path. I've only had these issues on Mac OS X, though, and never on my personal Ubuntu machine. I don't do node.js development in Windows, so I can't speak to that environment.
The problem I've encountered is that the module loads relative to where the require() function is invoked. Here's how you could easily solve that problem:
var settings = config.readConfig(require.resolve('./app.yaml'));
You might be able to hack something together using path.relative just to test if passing a relative path also resolves the issue. The path woule be relative to the location of the yaml-config module, and will most likely be something like ../../../src/app.yaml depending on your project's structure compared to yaml-config's lib structure.
If you want to play around with the relative stuff, it would look something like this (edit: fixed parentheses below):
var relative = path.relative(require.resolve('yaml-config'), './app.yaml');
var settings = config.readConfig(relative);