Command to create new directory in node.js
In Node.js, you can create a new directory using the fs
(File System) module. Here are a few ways to do it:
Method 1: Using fs.mkdir()
const fs = require('fs');
fs.mkdir('path/to/new/directory', (err) => {
if (err) {
console.error(err);
} else {
console.log('Directory created successfully!');
}
});
Replace 'path/to/new/directory'
with the desired path and name of the new directory.
Method 2: Using fs/promises
const fs = require('fs').promises;
fs.mkdir('path/to/new/directory')
.then(() => {
console.log('Directory created successfully!');
})
.catch((err) => {
console.error(err);
});
This method is similar to the first one, but uses the fs.promises
API, which returns a promise that resolves when the operation is complete.
Method 3: Using mkdir
from path
module
const path = require('path');
const fs = require('fs');
path.mkdir('path/to/new/directory', (err) => {
if (err) {
console.error(err);
} else {
console.log('Directory created successfully!');
}
});
This method is similar to the first one, but uses the path.mkdir()
method from the path
module.
Note that in all cases, you need to replace 'path/to/new/directory'
with the actual path and name of the directory you want to create.