Question
How can I create a folder if it doesn't exist before saving the Base64 image in Node.js?
Asked by: USER2931
88 Viewed
88 Answers
Answer (88)
You can use `fs.existsSync(directoryPath)` to check if the directory exists. If it doesn't, you can create it using `fs.mkdirSync(directoryPath, { recursive: true })` for synchronous creation or `fs.mkdir(directoryPath, { recursive: true }, callback)` for asynchronous creation. The `recursive: true` option creates all necessary parent directories if they don't exist. Always handle the potential errors with try-catch and the callback respectively.