node.jssharpavif

unable to open for write unix error: No such file or directory. How to fix?


I'm trying to convert png to avif via Sharp, but I'm getting an error.

Code example

const image = sharp(`./images/img/original/image.png`)
    .avif({quality: 70, loseless: true})
    .toFile(`/images/img/avif/out.avif`)

Error

[Error: /images/img/avif/6znl4e0.avif: unable to open for write unix error: No such file or directory

How i can fix it?


Solution

  • Sharp doesn't create a directory if it doesn't exist, You should check if the directory exists, and create it (using fs, for example).

    Here's an answer from : Github