How to create a directory if it doesn't exist using Node.js
In Node.js, you can use the fs
(file system) module to create a directory if it doesn't exist. The fs
module provides the mkdir
function for creating directories. Here's an example:
const fs = require('fs');
const directoryPath = './myDirectory';
// Check if the directory exists
if (!fs.existsSync(directoryPath)) {
// If it doesn't exist, create the directory
fs.mkdirSync(directoryPath);
console.log(`Directory '${directoryPath}' created.`);
} else {
console.log(`Directory '${directoryPath}' already exists.`);
}
In this example:
- The
fs.existsSync()
function checks if the directory already exists. - If the directory doesn't exist (
!fs.existsSync(directoryPath)
), thefs.mkdirSync()
function is used to create the directory synchronously. - If the directory already exists, a message is logged indicating that the directory is already present.
If you prefer an asynchronous approach, you can use the fs.promises.mkdir()
function:
const fs = require('fs').promises;
const directoryPath = './myDirectory';
// Use fs.promises.mkdir() to create the directory asynchronously
fs.promises.mkdir(directoryPath)
.then(() => console.log(`Directory '${directoryPath}' created.`))
.catch(err => console.error(`Error creating directory: ${err.message}`));
Note that the asynchronous method returns a promise, and you can use .then()
to handle success and .catch()
to handle errors.
Choose the synchronous or asynchronous approach based on your application's requirements. If you're working with other asynchronous operations, using the asynchronous method might be more appropriate to avoid blocking the event loop.
-
How do you get a list of the names of all files present in a directory in Node.js?
In Node.js, you can use the fs (file system) module to get a list of file names in a directory. Here's an example using the fs.readdir function: const fs = require('fs'); const directoryPath = '/pa...
Questions -
What is the purpose of Node.js module.exports and how do you use it?
In Node.js, module.exports is a special object that is used to define what a module exports as its public interface. It is used to expose functionality from one module (file) to another module, all...
Questions -
Using Node.js require vs. ES6 import/export
Node.js uses CommonJS-style require for module loading, while ES6 introduces a native import and export syntax. Each has its own style and use cases. Using require (CommonJS): // Importing a module...
Questions -
How can I update Node.js and NPM to their latest versions?
There are several ways to update Node.js to its latest version. Here are three methods: Updating Node.js Using NPM You can use NPM to update Node.js by installing the n package, which will be used ...
Questions
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github