Skip to content

Nodejs – Determine root path of application

Simple ways to determine root path of your node.js applications:

  • Add this somewhere towards the start of your main app file: app.js
global.__basedir = __dirname;

Use it just like any other global variables:

folderPath = __basedir + '/storage/data.json';
// or
const userModule = require(__basedir + '/modules/user.module.js');
  • NodeJS.Process.cwd():
const process = require('process');
...
const __basedir = process.cwd();
const userModule = require(__basedir + '/modules/user.module.js');
Published inCodingNodejs

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *