简体   繁体   中英

How to create directory outside project root directory with node.js?

Iam new here.. I am trying to create a directory outside of root project directory where i will read and write from a file created by library automatically. Like i need to create a directory in "/home/user/Documents/newdirectory" Iam working with linux.

I tried with below code with node.js ,but it is creating a directory at the project folders itself. Can anyone plz suggest on this?


fs.mkdir("./files/a/new-directory-name", { recursive: true }, function(err) {
  if (err) {
    console.log(err)
  } else {
    console.log("New directory successfully created.")
  }
})



>It is created a directory but in root project directory. 
But I need to create a dirwctory at $Home/user/Documents/newdirectory.

A path that begins with . is a relative path. Node fs uses process.cwd() as the root for relative paths.

If you need to create a directory at /home/user/Documents/newdirectory , then that's exactly what you should pass in to fs.mkdir():

fs.mkdir("/home/user/Documents/newdirectory", ...

Can anybody support on this ? I have to create a directory in $HOME/$USER/Documents in linux and then read and write this directory?

Should i use os.homedir to create a dir and then with chmod to pass permissions?

I have to read and write the absolute path of directory in home directory. But fs.mkdir is using only relative path if i write absolute path it gives error.

Plz support

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM