node js create or check directory
To create or check a directory in Node.js, you can use the built-in fs
module. Here are the steps to create or check a directory:
- Import the
fs
module:
const fs = require('fs');
- To create a directory, use the
mkdir
method:
fs.mkdir(path, options, callback);
path
: The path of the directory you want to create.options
(optional): An object that specifies the directory's permissions.callback
(optional): A function that is called after the directory is created or if an error occurs.To check if a directory exists, use the
access
method:
fs.access(path, mode, callback);
path
: The path of the directory you want to check.mode
(optional): The constant that specifies the accessibility check. By default, it is set tofs.constants.F_OK
.callback
: A function that is called with an error if the directory does not exist or if it is not accessible.
Please note that the fs
module provides various other methods for working with directories and files in Node.js, such as readdir
, rmdir
, unlink
, etc. You can explore these methods in the Node.js documentation for further functionality.