Open In App

Node process.cwd() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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




// 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: Below examples illustrate the use of process.cwd() method in Node.js:

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

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
Previous
Next
Share your thoughts in the comments
Similar Reads