Sharp: Error: vips__file_open_write: unable to open file for writing , unix error: No such file or directory

Created on 11 Oct 2019  路  2Comments  路  Source: lovell/sharp

here i am trying to get metadata and generate diffrent size of images but giving me error like this.
it will be really great if someone could give solution.

Error: vips__file_open_write: unable to open file for writing
unix error: No such file or directory

const imageToProcess = sharp(image);
await imageToProcess.metadata().then(async (metadata) => {
const fileRect = {
height: metadata.height,
width: metadata.width,
};
if (metadata.orientation && metadata.orientation >= 4) {
// Swap hieght and width for calculation
fileRect.height = metadata.width;
fileRect.width = metadata.height;
}
const rect = this.getRect(fileRect, thumb);
const newImage = await imageToProcess.rotate().jpeg().resize(rect.width, rect.height);
newImage.
toFile(_thumb.thumbPath, (err) => {
if (!err) {
// do something
} else {
console.log(err);
}
});
}, (error) => {
console.log(error);
});

question

Most helpful comment

Sharp doesn't create a directory if it doesn't exist, so if _thumb.thumbPath points to a non-existing directory libvips will throw an error. You should check if the directory exists, and create it (using fs, for example) if it doesn't.

Note that fs currently recommends writing to the file first and then handling any file-not-found errors to prevent race conditions where the file or directory is deleted between checking and writing: https://nodejs.org/api/fs.html#fs_fs_exists_path_callback

Alternatively you could use a package like mkdirp to create the directory for you, if you're so inclined.

Also see #639 #874 #1052 #1765

All 2 comments

Sharp doesn't create a directory if it doesn't exist, so if _thumb.thumbPath points to a non-existing directory libvips will throw an error. You should check if the directory exists, and create it (using fs, for example) if it doesn't.

Note that fs currently recommends writing to the file first and then handling any file-not-found errors to prevent race conditions where the file or directory is deleted between checking and writing: https://nodejs.org/api/fs.html#fs_fs_exists_path_callback

Alternatively you could use a package like mkdirp to create the directory for you, if you're so inclined.

Also see #639 #874 #1052 #1765

Thank you @Fdebijl. it was helpfull to me, Actually directory was already created but i had a problem with path.

Was this page helpful?
0 / 5 - 0 ratings