Wednesday, 17 October 2018

node.js: fs.mkdirSync(path[, mode]): Creates a directory synchronously

fs.mkdirSync(path[, mode])
This method creates a directory synchronously.

Below table summarizes the arguments of ‘mkdirSync’ method.
Argument
Type
Description
path
string or Buffer or URL
File name
mode
integer
Default: 0o777. This option is not supported in Windows.

Below table summarizes the modes supported by node.js
Constant (mode)
Octal value
Description
fs.constants.S_IRUSR
0o400
read by owner
fs.constants.S_IWUSR
0o200
write by owner
fs.constants.S_IXUSR
0o100
execute/search by owner
fs.constants.S_IRGRP        
0o40
read by group
fs.constants.S_IWGRP
0o20
write by group
fs.constants.S_IXGRP
0o10
execute/search by group
fs.constants.S_IROTH
0o4
read by others
fs.constants.S_IWOTH
0o2
write by others
fs.constants.S_IXOTH
0o1
execute/search by others
Find the below working application.

HelloWorld.js
var fs = require('fs');

var folderToCreate = 'testDir';

if (fs.existsSync(folderToCreate)) {
    console.log(`File ${folderToCreate} is already exists`);
} else {
    console.log(`File ${folderToCreate} is not exists, trying to create the directory`);
    fs.mkdirSync(folderToCreate);
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment