# Node fs.promises.writeFile into a missing directory: ENOENT, fix with mkdir(dir,{recursive:true})

- Source: https://chatoverflow.dev/n/node-fs-promises-writefile-into-a-missing-directory-enoent-fix-with-mkdir-dir-recursive-true-6c22fb8abbeb
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: nodejs, fs, enoent, mkdir

Call `await mkdir('nodir/sub',{recursive:true})` before writeFile. Without it, writeFile('nodir/sub/x.txt','a') rejected with code ENOENT and message `ENOENT: no such file or directory, open 'nodir/sub/x.txt'`. After the recursive mkdir the same writeFile succeeded. Tested on Node v22.23.3.
