Is there a different way, other than process.cwd()
, to get the pathname of the current project's root-directory. Does Node implement something like ruby's property, Rails.root
,. I'm looking for something that is constant, and reliable.
We can get the path of the present script in node. js by using __dirname and __filename module scope variables. __dirname: It returns the directory name of the current module in which the current script is located. __filename: It returns the file name of the current module.
__dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file.
The __dirname or __filename global variables are not available in ECMAScript module files. To solve the "__dirname is not defined in ES module scope" error, import and use the dirname() method from the path module. The dirname method takes a path as a parameter and returns the directory name of the path.
process. cwd() returns the current working directory, i.e. the directory from which you invoked the node command. __dirname returns the directory name of the directory containing the JavaScript source code file.
There are many ways to approach this, each with their own pros and cons:
require.main.filename
From http://nodejs.org/api/modules.html:
When a file is run directly from Node,
require.main
is set to itsmodule
. That means that you can determine whether a file has been run directly by testingrequire.main === module
Because
module
provides afilename
property (normally equivalent to__filename
), the entry point of the current application can be obtained by checkingrequire.main.filename
.
So if you want the base directory for your app, you can do:
const { dirname } = require('path'); const appDir = dirname(require.main.filename);
This will work great most of the time, but if you're running your app with a launcher like pm2 or running mocha tests, this method will fail.
module.paths
Node publishes all the module search paths to module.paths
. We can traverse these and pick the first one that resolves.
async function getAppPath() { const { dirname } = require('path'); const { constants, promises: { access } } = require('fs'); for (let path of module.paths) { try { await access(path, constants.F_OK); return dirname(path); } catch (e) { // Just move on to next path } } }
This will sometimes work, but is not reliable when used in a package because it may return the directory that the package is installed in rather than the directory that the application is installed in.
Node has a global namespace object called global
— anything that you attach to this object will be available everywhere in your app. So, in your index.js
(or app.js
or whatever your main app file is named), you can just define a global variable:
// index.js var path = require('path'); global.appRoot = path.resolve(__dirname); // lib/moduleA/component1.js require(appRoot + '/lib/moduleB/component2.js');
Works consistently, but you have to rely on a global variable, which means that you can't easily reuse components/etc.
process.cwd()
This returns the current working directory. Not reliable at all, as it's entirely dependent on what directory the process was launched from:
$ cd /home/demo/ $ mkdir subdir $ echo "console.log(process.cwd());" > subdir/demo.js $ node subdir/demo.js /home/demo $ cd subdir $ node demo.js /home/demo/subdir
To address this issue, I've created a node module called app-root-path. Usage is simple:
const appRoot = require('app-root-path'); const myModule = require(`${ appRoot }/lib/my-module.js`);
The app-root-path module uses several techniques to determine the root path of the app, taking into account globally installed modules (for example, if your app is running in /var/www/
but the module is installed in ~/.nvm/v0.x.x/lib/node/
). It won't work 100% of the time, but it's going to work in most common scenarios.
Works without configuration in most circumstances. Also provides some nice additional convenience methods (see project page). The biggest con is that it won't work if:
node_modules
directory (for example, if you installed it globally)You can get around this by either setting a APP_ROOT_PATH
environmental variable, or by calling .setPath()
on the module, but in that case, you're probably better off using the global
method.
If you're looking for a way to determine the root path of the current app, one of the above solutions is likely to work best for you. If, on the other hand, you're trying to solve the problem of loading app modules reliably, I highly recommend looking into the NODE_PATH
environmental variable.
Node's Modules system looks for modules in a variety of locations. One of these locations is wherever process.env.NODE_PATH
points. If you set this environmental variable, then you can require
modules with the standard module loader without any other changes.
For example, if you set NODE_PATH
to /var/www/lib
, the the following would work just fine:
require('module2/component.js'); // ^ looks for /var/www/lib/module2/component.js
A great way to do this is using npm
:
{ "scripts": { "start": "NODE_PATH=. node app.js" } }
Now you can start your app with npm start
and you're golden. I combine this with my enforce-node-path module, which prevents accidentally loading the app without NODE_PATH
set. For even more control over enforcing environmental variables, see checkenv.
One gotcha: NODE_PATH
must be set outside of the node app. You cannot do something like process.env.NODE_PATH = path.resolve(__dirname)
because the module loader caches the list of directories it will search before your app runs.
[added 4/6/16] Another really promising module that attempts to solve this problem is wavy.
__dirname
isn't a global; it's local to the current module so each file has its own local, different value.
If you want the root directory of the running process, you probably do want to use process.cwd()
.
If you want predictability and reliability, then you probably need to make it a requirement of your application that a certain environment variable is set. Your app looks for MY_APP_HOME
(Or whatever) and if it's there, and the application exists in that directory then all is well. If it is undefined or the directory doesn't contain your application then it should exit with an error prompting the user to create the variable. It could be set as a part of an install process.
You can read environment variables in node with something like process.env.MY_ENV_VARIABLE
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With