How would I get the path to the script in Node.js?
I know there's
process.cwd, but that only refers to the directory where the script was called, not of the script itself. For instance, say I'm in
/home/kyle/ and I run the following command:
If I call
process.cwd(), I get
/home/kyle/some/dir/. Is there a way to get that directory?
__filenameis the file name of the current module. This is the resolved absolute path of the current module file. (ex:
__dirnameis the directory name of the current module. (ex:
So basically you can do this:
fs.readFile(path.resolve(__dirname, 'settings.json'), 'UTF-8', callback);
Use resolve() instead of concatenating with '/' or '\' else you will run into cross-platform issues.
Note: __dirname is the local path of the module or included script. If you are writing a plugin which needs to know the path of the main script it is:
or, to just get the folder name: