node.js remove file

前端 未结 17 1470
误落风尘
误落风尘 2020-12-07 06:54

How do I delete a file with node.js?

http://nodejs.org/api/fs.html#fs_fs_rename_oldpath_newpath_callback

I don\'t see a remove command?

17条回答
  •  温柔的废话
    2020-12-07 07:29

    You may use fs.unlink(path, callback) function. Here is an example of the function wrapper with "error-back" pattern:

    // Dependencies.
    const fs = require('fs');
    
    // Delete a file.
    const deleteFile = (filePath, callback) => {
      // Unlink the file.
      fs.unlink(filePath, (error) => {
        if (!error) {
          callback(false);
        } else {
          callback('Error deleting the file');
        }
      })
    };

提交回复
热议问题