nodejs check directory exist or not
To check if a directory exists or not in Node.js, you can use the fs
module, which is a built-in module for handling file system operations. Specifically, you can use the fs.existsSync()
function.
Here are the steps to check if a directory exists in Node.js:
- Import the
fs
module using therequire()
function:
const fs = require('fs');
- Use the
fs.existsSync()
function to check if the directory exists:
const directoryPath = '/path/to/directory';
const exists = fs.existsSync(directoryPath);
- The
fs.existsSync()
function returns a boolean value. If the directory exists, it will returntrue
; otherwise, it will returnfalse
. - You can then use the
exists
variable to perform further actions based on whether the directory exists or not.
That's it! These steps will allow you to check if a directory exists or not in Node.js using the fs.existsSync()
function.