3

I am trying to create the directory in public/data folder to place my read data but Getting this error while creating the directory dynamically in nodejs.

Error: ENOENT: no such file or directory, mkdir './public/data/folder'
    at Object.mkdirSync (fs.js:753:3)
    at /home/ubuntu/New/routes/index.js:589:14
    at FSReqWrap.oncomplete (fs.js:141:20)

I have implemented my code like this by checking the folder exist or not and creating the folder when not exist using mkdirsync()

var dd = './public/data/'+ id;
 if (!fs.existsSync(dd)) 
        {
          fs.mkdirSync(dd,'0777', true);
          console.log("Directory Created!!");
      }

Directory Created!!
Avinash Singh
  • 4,970
  • 8
  • 20
  • 35
gopinath
  • 61
  • 1
  • 11
  • 1
    Possible duplicate of [How to create full path with node's fs.mkdirSync?](https://stackoverflow.com/questions/31645738/how-to-create-full-path-with-nodes-fs-mkdirsync) – Shibon Apr 02 '19 at 04:58
  • I think you should check if intermediate directories also exist because it is possible that public or public/data may not exist – Daniel Jee Apr 02 '19 at 05:16

2 Answers2

2

mkdirSync accepts an option property recursive (default is false). Check out the guide

So you could use:

fs.mkdirSync('/dir1/dir2', { recursive: true });
wuarmin
  • 3,274
  • 3
  • 18
  • 31
  • Getting same error `ENOENT: no such file or directory, mkdir '/dir1/dir2'`, running from build. – 151291 Aug 19 '20 at 15:53
-1

This solved issue, Running the npm with root permission

Solution:

sudo npm install -g PACKAGE-NAME --unsafe-perm=true --allow-root

gopinath
  • 61
  • 1
  • 11