fs.mkdir vs. fs.mkdirSync in Node.js

Key takeaways:

  • mkdir() and mkdirSync() create directories with key differences in behavior.

  • mkdir() is asynchronous. It doesn’t block code execution and works well for non-blocking requests.

  • mkdirSync() is synchronous. It blocks execution until completion, useful for ensuring immediate directory creation.

  • Use if–else for handling mkdir() cases and try–catch for mkdirSync().

  • Syntax for mkdir():

    • fs.mkdir('./path/directoryName', callback)

  • Syntax for mkdirSync():

    • fs.mkdirSync('./path/directoryName')

  • Import the fs module to access these methods: const fs = require('fs');

The fs module is a built-in file system module in Node.js that consists of several methods that can be used to access and manipulate files on the operating system. mkdir() and mkdirSync() are fs methods used to create a new directory.

A new directory is created.
A new directory is created.

Differences between fs.mkdir and fs.mkdirSync

The main difference between the two methods is that mkdir() is an asynchronous method and mkdirSync() is an asynchronous method.

Let’s look at their key differences and get a basic idea regarding when to use which method.

fs.mkdir vs. fs.mkdirSync


mkdir()

mkdirSync()

Asynchronous

vs.

Synchronous

It is asynchronous that means when this function is called, it will execute and create a new directory while the other code continues to execute.

It is synchronous that means when this function is called, it will execute and create a new directory and send a callback. Till then the system will go in waiting and nothing else is executed.

Syntax

Use if-else to handle cases.

Use try-catch to handle cases.

When to Use

Used when there is a need to handle non-blocking requests. Hence, no immediate need to create the directory.


Used where there is a need for a blocking request to ensure immediate directory creation.

Real Life Example

We can upload files asynchronously on a web server, allowing the server to simultaneously handle other requests.

We can create directories synchronously when handling nested files and directory scenarios to avoid any race conditions.

Required import

Import the Node.js package fs to access files:

const fs = require('fs')

Syntax

The parameters for the fs.mkdir() and fs.mkdirSync() methods are the same, with a slight difference in syntax.

The syntax for fs.mkdir() when handling its cases using if–else conditions:

fs.mkdir('./path/directoryName', (err) => {
if (err) {
console.error(err);
} else {
console.log('Directory created successfully.');
}
});

The syntax for fs.mkdirSync() when handling its cases using try–catch block:

try {
fs.mkdirSync('./path/directoryName');
console.log('Directory created successfully.');
} catch (err) {
console.error(err);
}

Parameters

  • path: The destination of the directory that is to be created. For example, if we want to create it inside a NewDir directory, we will write NewDir in place of the path. This helps in creating nested directories.

  • directoryNameThe name of the directory that we want to create.

  •  err: A callback function that is called once the method is executed and returns one of the two cases:

    • error: The method fails to create the specified directory.

    • null: The method successfully creates the specified directory.

Example code

This code creates a directory using both methods to practically communicate the difference between their implementation.

Explains the difference between two methods through console outputs.

Output explanation

Let’s understand the output of the above example code and examine the difference in the behavior of a synchronous and asynchronous function.

Output of the example above.
Output of the example above.
  • Message 1 is printed because it is the first statement in the code.

  • Message 2 is printed, which is the very following statement after the mkdir() method is called.

  • A new directory is created through the mkdirSync() method, and its successful case statement is immediately printed because synchronous commands do not allow the code to proceed until its callback is received.

  • Message 3 is printed as it is the very following statement after the mkdirSync() method is called.

  • A new SyncDirectory directory is added to the public directory, which was created through the mkdirSync() method. Note that the synchronous method is executed before the asynchronous method.

  • A new AsyncDirectory directory is added to the public directory, which was created through the mkdir() method.

  • The success case message for the directory created through the mkdir() method is printed at the end once the whole code is executed because it is an asynchronous command.

Test your understanding

A quick quiz to test your understanding.

Q
try {
fs.mkdirSync('./Dir1');
console.log('Dir1 created successfully.');
try{
  fs.mkdirSync('./Dir2');
  console.log('Dir2 created successfully.');
}catch(err) {
  console.log(err);
}} catch (err) {
console.error(err);}

What will be the console output for this code?

A)

Dir1 created successfully.

Dir2 created successfully.

B)

Dir2 created successfully.

Dir1 created successfully.

Conclusion

In conclusion, the fs module in Node.js offers powerful methods for managing directories, with mkdir() and mkdirSync() serving as key options for creating directories. Understanding their asynchronous and synchronous nature is crucial for selecting the right method based on your application’s needs. Use mkdir() for non-blocking operations where performance is prioritized, and mkdirSync() for scenarios requiring immediate execution and strict sequence control. By using these methods effectively, you can handle file system operations with precision and efficiency in your Node.js applications.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is the stream fs.readdir() method in Node.js?

fs.readdir() reads the contents of a directory asynchronously and returns an array of file and folder names through a callback.

Want to learn more about fs.readdir(), check out our Answer: What is the stream fs.readdir() method in Node.js?


What is difference between fs.rename and fs.renameSync in Node.js?

  • fs.rename: Asynchronous, renames or moves a file and requires a callback for completion.
  • fs.renameSync: Synchronous, performs the operation and blocks further code execution until completion.

To learn more, read our Answer on fs.rename vs. fs.renameSync in Node.js


What is difference between fs.stat and fs.statSync in Node.js?

  • fs.stat: Asynchronous, retrieves file or directory information and uses a callback for the result.
  • fs.statSync: Synchronous, retrieves information and blocks code execution until completion.

For details, see our Answer on fs.stat vs. fs.statSync in Node.js


What is the difference between fs.rmdir and fs.rmdirSync in Node.js?

  • fs.rmdir: Asynchronous, removes an empty directory and requires a callback for handling results.
  • fs.rmdirSync: Synchronous, removes an empty directory and blocks further execution until the operation completes.

Learn more in our Answer: fs.rmdir vs. fs.rmdirSync in Node.js


What is rimraf.sync() in Node.js?

rimraf.sync() is a utility method for synchronously deleting files or directories, including non-empty ones, often used as an alternative to fs.rmdirSync for recursive deletion.

For a deeper understanding, check out our Answer on What is rimraf.sync() in Node.js?


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved