Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Node.js process.chdir() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The process.chdir() method is an inbuilt application programming interface of the process module which is used to change the current working directory. Syntax:

process.chdir( directory )

Parameters: This method accepts single parameter as mentioned above and described below:

  • directory: It is required parameter that specifies the path to the directory to which current working directory to be changed.

Return Value: This method does not return any value on success but throws an exception if fails to change directory specifying that “no such file or directory”. Below examples illustrate the use of process.chdir() method in Node.js: Example 1: 

javascript




// Node.js program to demonstrate the    
// process.chdir() Method
  
// Include process module
const process = require('process');
 
try {
 
  // Change the directory
  process.chdir('../os');
  console.log("directory has successfully been changed");
} catch (err) {
     
  // Printing error if occurs
  console.error("error while changing directory");
}

Output:

directory has successfully been changed

Example 2: 

javascript




// Node.js program to demonstrate the    
// process.chdir() Method
  
// Include process module
const process = require('process');
 
// Printing current directory
console.log("current working directory: "
          + process.cwd());
try {
     
  // Change the directory
  process.chdir('../os');
  console.log("working directory after "
          + "changing: " + 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: 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_chdir_directory


My Personal Notes arrow_drop_up
Last Updated : 10 Jun, 2022
Like Article
Save Article
Similar Reads
Related Tutorials