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. Below examples illustrate the use of process.cwd() method in Node.js: Example 1:
javascript
const process = require( 'process' );
console.log("Current working directory: ",
process.cwd());
|
Output:
Current working directory: C:\nodejs\g\process
Example 2:
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
Note: The above program will compile and run by using the node filename.js command. Reference: https://nodejs.org/api/process.html#process_process_cwd