node.js我的configuration文件在哪里?

$ 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' ] 

下面的node.js代码的输出

 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); 

布兰登,公平点。 我正在使用npm页面的例子。 我简化了代码,并合并了我的文件系统检查。

我以前遇到过使用require()和相对pathdynamic地“加载”configuration文件的模块问题。 我只在Mac OS X上遇到过这些问题,从来没有在我的个人Ubuntu机器上。 我不在Windows中做node.js开发,所以我不能说这个环境。

我遇到的问题是,该模块相对于require()函数被调用的地方加载 。 以下是您可以轻松解决该问题的方法:

 var settings = config.readConfig(require.resolve('./app.yaml')); 

你可能能够使用path.relative一起破解一些东西,只是为了testing传递相对path是否也能解决问题。 这个path与yaml-config模块的位置有关,很可能像../../../src/app.yaml取决于你项目的结构与yaml-config的lib结构。

如果你想玩相关的东西,它会看起来像这样(编辑:下面的固定括号):

 var relative = path.relative(require.resolve('yaml-config'), './app.yaml'); var settings = config.readConfig(relative);