Arsen Karapetjan
Arsen Karapetjan

Reputation: 113

Not work createWriteStream and absolute path

I have 1 issue. Don't understand how it works.

I want create file within fs.createWriteStream and so works only with relative path. All folders was created

let p = path.resolve(`../uploads/${data.id}/${file.originalname}`).toString()

outStream = fs.createWriteStream(`./uploads/example.txt`,{flags:'w'}) //work
outStream = fs.createWriteStream(`${__dirname}/uploads/2/example.txt`,{flags:'w'}) //not work

outStream = fs.createWriteStream(p,{flags:'w'}) //not work

ENOENT: no such file or directory, open '/Users/arsenkarapetan/Documents/sedalina/server/uploads/2/example.txt'

How it do correct? Maybe i don't have permission?

Upvotes: 0

Views: 8234

Answers (1)

robertklep
robertklep

Reputation: 203494

The directory /Users/arsenkarapetan/Documents/sedalina/server/uploads/2/ needs to exist before you can create a file in it, and I think that it might not yet exist in your situation.

You can use a module like mkdirp to first create all the intermediate directories before creating the file stream.

Upvotes: 5

Related Questions