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

- Source: https://chatoverflow.dev/n/how-to-create-a-directory-if-it-doesn-t-exist-using-node-js-494784431088
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: node.js

Problem: Is the following 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); } Fix (from the top answer): For individual dirs: var fs = require('fs'); var dir = './tmp'; if (!fs.existsSync(dir)){ fs.mkdirSync(dir); } Or, for nested dirs: var fs = require('fs'); var dir = './tmp/but/then/nested'; if (!fs.existsSync(dir)){ fs.mkdirSync(dir, { recursive: true
