node.js remove file

前端 未结 17 1456
误落风尘
误落风尘 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:27

    2019 and Node 10+ is here. Below the version using sweet async/await way.

    Now no need to wrap fs.unlink into Promises nor to use additional packages (like fs-extra) anymore.

    Just use native fs Promises API.

    const fs = require('fs').promises;
    
    (async () => {
      try {
        await fs.unlink('~/any/file');
      } catch (e) {
        // file doesn't exist, no permissions, etc..
        // full list of possible errors is here 
        // http://man7.org/linux/man-pages/man2/unlink.2.html#ERRORS
        console.log(e);
      }
    })();
    

    Here is fsPromises.unlink spec from Node docs.

    Also please note that fs.promises API marked as experimental in Node 10.x.x (but works totally fine, though), and no longer experimental since 11.14.0.

    0 讨论(0)
  • 2020-12-07 07:29

    You can call fs.unlink(path, callback) for Asynchronous unlink(2) or fs.unlinkSync(path) for Synchronous unlink(2).
    Where path is file-path which you want to remove.

    For example we want to remove discovery.docx file from c:/book directory. So my file-path is c:/book/discovery.docx. So code for removing that file will be,

    var fs = require('fs');
    var filePath = 'c:/book/discovery.docx'; 
    fs.unlinkSync(filePath);
    
    0 讨论(0)
  • 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');
        }
      })
    };

    0 讨论(0)
  • 2020-12-07 07:29

    It's very easy with fs.

    var fs = require('fs');
    try{
     var sourceUrls = "/sampleFolder/sampleFile.txt";
     fs.unlinkSync(sourceUrls);
    }catch(err){
     console.log(err);
    }
    
    0 讨论(0)
  • 2020-12-07 07:33

    Here is a small snippet of I made for this purpose,

    var fs = require('fs');
    var gutil = require('gulp-util');
    
    fs.exists('./www/index.html', function(exists) {
      if(exists) {
        //Show in green
        console.log(gutil.colors.green('File exists. Deleting now ...'));
        fs.unlink('./www/index.html');
      } else {
        //Show in red
        console.log(gutil.colors.red('File not found, so not deleting.'));
      }
    });
    
    0 讨论(0)
  • 2020-12-07 07:33

    As the accepted answer, use fs.unlink to delete files.

    But according to Node.js documentation

    Using fs.stat() to check for the existence of a file before calling fs.open(), fs.readFile() or fs.writeFile() is not recommended. Instead, user code should open/read/write the file directly and handle the error raised if the file is not available.

    To check if a file exists without manipulating it afterwards, fs.access() is recommended.

    to check files can be deleted or not, Use fs.access instead

    fs.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK, (err) => {
      console.log(err ? 'no access!' : 'can read/write');
    });
    
    0 讨论(0)
提交回复
热议问题