dominictarr / rc

The non-configurable configuration loader for lazy people.
Other
1.02k stars 97 forks source link

Can't find config in local dir #37

Closed anthonator closed 9 years ago

anthonator commented 9 years ago

I can't seem to get rc to behave like I assumed it would. I want to test loading my config by putting a .apprc file in my test directory. However, rc doesn't ever look there. I assumed it would look in the directory where the code was executing.

I have a project with the following directory structure:

/ +
  |
  -- lib +
        |
        -- loadConfig.js
  |
  -- test +
        |
        -- .apprc
        |
        -- testLoadConfig.js

loadConfig.js

var conf = require('rc')('app');

module.exports = function(options) {
  console.log(conf);

  return options;
};

testLoadConfig.js

var assert = require('assert');

var loadConfig = require('../lib/loadConfig');

describe('loadConfig', function() {
  it('should load rc config', function() {
    var config = loadConfig(); // assumed it would look for .apprc in project/test/.apprc

    console.log(config); // outputs { _: [] }
  });
});

I figure this is either designed this way intentionally or is a technical limitation but it's causing some heartburn so I thought I'd bring it up.

dominictarr commented 9 years ago

Thanks for posting. it starts local to where you run the command from. if you are run this command: cd ~/project; app test/* and app uses rc then it will see ~/project/.apprc but not ~project/test/.apprc

basically, it's because in node you can see your own filename and directory (__dirname and __filename) and the processes current directory process.cwd()

The process's current working directory is readily acessable in unix, but not always the "__filename" (try it in bash!, especially once symlinks come into play!)

if you want to put default config relative to your actual code, you should put it inside your code. normally I have a config.js file that sets up rc with defaults and can be required by other parts of by application. here is an example: https://github.com/ssbc/scuttlebot/blob/master/config.js