NodeJS get rootpath of our project
To get the root path of a Node.js project, you can use the process.cwd()
method. This method returns the current working directory, which is the root path of the project.
Here's how you can get the root path of a Node.js project in steps:
- Import the
path
module:
const path = require('path');
- Use the
process.cwd()
method to get the current working directory:
const rootPath = process.cwd();
- (Optional) If you want to resolve a specific file or directory relative to the root path, you can use the
path.resolve()
method:
const specificPath = path.resolve(rootPath, 'path/to/file');
By following these steps, you can easily retrieve the root path of your Node.js project.