• There are multiple ways to find root directory of we can say current path of directory
  • First way: You can use process.cwd() method for getting current directory
  • Second way: Using path npm module

Example:

const { dirname } = require('path');
const appDir = dirname(require.main.filename);

console.log(appDir)
  • Third way: You can user app-root-path npm module

Example:

const appRoot = require('app-root-path');
console.log(JSON.stringify(appRoot))

OUTPUT:-
{"path":"C:\\Users\\admin\\Desktop\\nodejs"}

Support On Demand!

Node