Node process.cwd() Method
The process.cwd() method is an inbuilt application programming interface of the process module which is used to get the current working directory of the node.js process.
Syntax:
process.cwd()
Parameters: This method does not accept any parameters. Return Value: This method returns a string specifying the current working directory of the node.js process.
Example 1: Below examples illustrate the use of process.cwd() method in Node.js:
Javascript
const process = require( 'process' );
console.log( "Current working directory: " ,
process.cwd());
|
Output:
Current working directory: C:\nodejs\g\process
Example 2: Below examples illustrate the use of process.cwd() method in Node.js:
Javascript
const process = require( 'process' );
console.log( "Current working directory: " ,
process.cwd());
try {
process.chdir( '../' );
console.log( "Working directory after changing"
+ " directory: " , process.cwd());
} catch (err) {
console.error( "error occurred while changing"
+ " directory: " , err);
}
|
Output:
Current working directory: C:\nodejs\g\process
Working directory after changing directory: C:\nodejs\g\os
We have a Cheat Sheet on Node Process Methods where we covered all the process methods to check those please go through Node Process Complete Reference this article.
Last Updated :
15 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...