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!!
2
  • 1
    Possible duplicate of How to create full path with node's fs.mkdirSync? Commented Apr 2, 2019 at 4:58
  • I think you should check if intermediate directories also exist because it is possible that public or public/data may not exist Commented Apr 2, 2019 at 5:16

2 Answers 2

2

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

So you could use:

fs.mkdirSync('/dir1/dir2', { recursive: true });
Sign up to request clarification or add additional context in comments.

1 Comment

Getting same error ENOENT: no such file or directory, mkdir '/dir1/dir2', running from build.
-1

This solved issue, Running the npm with root permission

Solution:

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

1 Comment

Please don't use root permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.