I am creating a node command line interface. It is installed globally and uses a bin file to execute.
I plan to have a command window open at the root directory of the files I am working on and then just run the command however I have been unable to determine the current working directory as
var path = require('path');
var fs = require('fs');
var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');
require(lib + '/test');
process.cwd()returns directory where command has been executed (not directory of the node package) if it's has not been changed by 'process.chdir' inside of application.
__filenamereturns absolute path to file where it is placed.
__dirnamereturns absolute path to directory of
If you need to load files from your module directory you need to use relative paths.
var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib'); require(lib + '/test');
It's always relative to file where it called from and don't depend on current work dir.