Skip to content
Related Articles

Related Articles

Node.js | process.cwd() Method
  • Last Updated : 25 Feb, 2020

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:




// 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:




// 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 occured 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 :