Sometimes, we want to change working directory in my current shell context when running a Node.js script.
In this article, we’ll look at how to change working directory in my current shell context when running a Node.js script.
How to change working directory in my current shell context when running a Node.js script?
To change working directory in my current shell context when running a Node.js script, we can use the process.chdir
method.
For instance, we write
console.log('Starting directory', process.cwd());
try {
process.chdir('/tmp');
console.log('New directory', process.cwd());
} catch (err) {
console.log(err);
}
to get the current working directory with process.cwd
.
Then we call process.chdir
to change the current working directory to /tmp
.
Conclusion
To change working directory in my current shell context when running a Node.js script, we can use the process.chdir
method.