Reputation: 1338
I'm trying to move an existing file into a new folder that doesn't exist. I tried:
var source = fs.createReadStream(file.thumbnail.path);
var dest = fs.createWriteStream('./public/uploads/'+ user._id + '/' + file.myfail.name);
source.pipe(dest);
However, I keep getting this error Error: ENOENT, open './public/uploads/553283d3216c3895055612dd/18f1b232024ac9d7a5d398dc9291e160.jpg'
I also tried using __dirname
but it doesn't seem to help.
I'm pretty sure it's a non-existant folder issue, but I'm not sure how to fix it.
PS: after checking if the folder doesn't exist, how do I create it?
thanks
Upvotes: 1
Views: 2184
Reputation: 2645
The ENOENT
error is because the file or folder does not exist.
The only way to get around this problem is to open a file that does exist. Perhaps the user's folder does not exist before you try this operation? Check to see if the folder exists.
Another thing to note is that Node.js has a path module that provides a lot of useful filepath tools.
var path = require( 'path' )
var destination = path.join( __dirname, 'public/uploads', user._id, file.myfile.name )
Upvotes: 1
Reputation: 491
You'll want to check if the sub-directory "./public/uploads/" + user._id exists.
If it doesn't exist create the directory before attempting to write out to a directory that doesn't exist.
Upvotes: 0