简体   繁体   中英

Node.js fs.unlinkSync removes files but not folders

I can remove all image files (.png, .jpg, etc...), but I can't remove folders...

This is my code blog:

router.post('/remove-file', async (req, res) => {

    const removeDocuments = await req.body.removeDocuments
    // removeDocuments[0] equals to '/assets/img/folder'
    // removeDocuments[1] equals to '/assets/img/image.jpg'

    if(removeDocuments){

        for (let i = 0; i < removeDocuments.length; i++) {
            fs.unlinkSync('.' + removeDocuments[i])
        }

        res.send({
            status: true
        })

    }

})

The error code I get:

(node:808) UnhandledPromiseRejectionWarning: Error: EPERM: operation not permitted, unlink './assets/img/folder'

Where is the problem I did? How to solve it?

Unlink removes a name from the filesystem, and the file it referenced. It doesn't remove directories, as that could create orphan files. Use rmSync with recursive: true to delete directories.

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