Skip to content
Related Articles

Related Articles

Node.js process.cwd() Method

View Discussion
Improve Article
Save Article
  • Last Updated : 10 Jun, 2022
View Discussion
Improve Article
Save Article

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




// Node.js program to demonstrate the   
// process.cwd() Method
 
// Include process module
const process = require('process');
 
// Printing current directory
console.log("Current working directory: ",
          process.cwd());


Output:

Current working directory: C:\nodejs\g\process

Example 2: 

javascript




// Node.js program to demonstrate the   
// process.cwd() Method
 
// Include process module
const process = require('process');
 
// Printing current directory
console.log("Current working directory: ",
          process.cwd());
 
try {
     
  // Change working directory
  process.chdir('../');
  console.log("Working directory after changing"
      + " directory: ", process.cwd());
} catch (err) {
    
  // printing error if occurs
  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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!