How to create a directory if it doesn t exist using Node js

0 votes

Is this the right way to create a directory if it doesn't exist. It should have full permission for the script and readable by others.

var dir = __dirname + '/upload';
if (!path.existsSync(dir)) {
    fs.mkdirSync(dir, 0744);
}
Jul 9, 2020 in Node-js by kartik
• 37,510 points
5,729 views

1 answer to this question.

0 votes

Hello @kartik,

Try:

var fs = require('fs');
var dir = './tmp';

if (!fs.existsSync(dir)){
    fs.mkdirSync(dir);
}

Hope it helps!!

To know more about it, enroll in Node.js training today.

Thank You!!

answered Jul 9, 2020 by Niroj
• 82,880 points

Related Questions In Node-js

0 votes
1 answer

How to update a value in a json file and save it through node.js?

Hello @kartik, It's particularly useful if you're concerned ...READ MORE

answered Oct 12, 2020 in Node-js by Niroj
• 82,880 points
25,347 views
0 votes
1 answer

How to download a file with Node.js without using third-party libraries?

Hii, You can create an HTTP GET request and pipe ...READ MORE

answered Nov 24, 2020 in Node-js by Niroj
• 82,880 points
1,058 views
0 votes
1 answer

How to print a stack trace in Node.js?

Hello @kartik, To print stacktrace of Error in console in ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,880 points
621 views
0 votes
1 answer

How to create an HTTPS server in Node.js?

Hello @kartik, The minimal setup for an HTTPS ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,880 points
1,513 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,880 points
3,714 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,880 points
2,924 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,880 points
832 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
772 views
0 votes
1 answer

How to create a directory if it doesn't exist using Node.js?

Hello @kartik, Try this: var fs = require('fs'); var dir ...READ MORE

answered Jul 20, 2020 in Node-js by Niroj
• 82,880 points
550 views
0 votes
1 answer

How to create a pair private/public keys using Node.js crypto?

Hello @kartik, nodejs v10.12 now supports this natively ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,880 points
4,488 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP