how can i write the name and the path of my directory that i will to create in the same node command, like this and remove the path in my script >
node ./stack/create.js ./path/folderName
i´m trying to create a directory with this script but i want to remove const path
in my script and set the path and name when i will to ran my script like this node ./stack/create.js ./path/folderName
my script
JavaScript
x
26
26
1
import fs from 'fs';
2
3
// path is where i will to create the directory and folderName is the name of the directory
4
5
// i want to write the name and path in my node command like this
6
7
**`node ./stack/create.js ./stack/folderName`**
8
9
// i want remove this const because i dont want to open the script and change the path and name i prefer to write the path and name when i will to ran the script
10
11
const path = "./stack/004";
12
13
fs.access(path, (error) => {
14
if (error) {
15
fs.mkdir(path, {recursive: true}, (error) => {
16
if (error) {
17
console.log(error);
18
} else {
19
console.log("New Directory created successfully !!");
20
}
21
});
22
} else {
23
console.log("Given Directory already exists !!");
24
}
25
});
26
i´m trying to create a directory with this script but i want to remove const path in my script and set the path and name when i will to ran my script like this node ./stack/create.js ./path/folderName
Advertisement
Answer
Try this way :
JavaScript
1
18
18
1
import fs from 'fs';
2
3
const path = process.argv[2];
4
5
fs.access(path, (error) => {
6
if (error) {
7
fs.mkdir(path, {recursive: true}, (error) => {
8
if (error) {
9
console.log(error);
10
} else {
11
console.log("New Directory created successfully !!");
12
}
13
});
14
} else {
15
console.log("Given Directory already exists !!");
16
}
17
});
18