Node.js is a popular JavaScript back-end running environment. You can use Node.js to easily manipulate the local file system. Typically, we need to delete files frequently, and Node.js provides multiple built-in methods to simplify this process. In this article, we will discuss how to delete files using Node.js.
Node.js has a built-in File System core module, which provides many synchronous and asynchronous methods to operate the file system. Among these methods, the fs.unlink() function is used to delete files. Next we use this method to demonstrate how to delete files.
The synchronization operation is performed sequentially, that is, it blocks the execution of the code until the operation is completed. In Node.js, we can use the fs.unlinkSync() function to delete files synchronously. The syntax of this function is as follows:
fs.unlinkSync(path)
wherepath
is the path of the file to be deleted. The sample code is as follows:
const fs = require('fs'); const path = './test.txt'; //删除文件 try { fs.unlinkSync(path); console.log(`${path}删除成功`); } catch (error) { console.log(`删除${path}失败:${error}`); }
Although synchronous operation may block the application, it may be more convenient in some scenarios, such as in unit testing.
The asynchronous API of Node.js will not block the execution of the application, but will notify us of the end of the operation through the callback function. In the asynchronous API, we can use the fs.unlink() function to delete files asynchronously. The syntax of this function is as follows:
fs.unlink(path, callback)
where,path
is the path of the file to be deleted,callback
is the callback function. When the file deletion operation is completed, the callback function will be called and the error message will be passed to the callback function as the first parameter. If there are no errors, the first parameter will be null or undefined. The sample code is as follows:
const fs = require('fs'); const path = './test.txt'; //删除文件 fs.unlink(path, (error) => { if(error) { console.log(`删除${path}失败:${error}`); } else { console.log(`${path}删除成功`); } })
Asynchronous APIs are generally more flexible than synchronous APIs because they do not block the execution of the application. In an asynchronous operation, we can perform other operations as needed and execute the callback function after the operation is completed.
Use the fs.rmdir() function to delete empty directories. If the directory is not empty, the delete operation will fail. The syntax of this function is as follows:
fs.rmdir(path, callback)
Among them,path
is the path of the directory to be deleted, andcallback
is the callback function. When the directory deletion operation is completed, the callback function will be called and the error message will be passed to the callback function as the first parameter. If there are no errors, the first parameter will be null or undefined.
If you want to delete non-empty directories, you can use the third-party module rimraf. rimraf provides a simple interface for deleting files and directories including non-empty directories. First, we need to install rimraf using npm:
npm install rimraf
Then, we can use the rimraf() function to delete non-empty directories. The syntax of this function is as follows:
const rimraf = require('rimraf'); rimraf(path, (error) => { if(error) { console.log(`删除${path}失败:${error}`); } else { console.log(`${path}删除成功`); } })
Among them,path
is the path of the directory to be deleted, andcallback
is the callback function. When the directory deletion operation is completed, the callback function will be called and the error message will be passed to the callback function as the first parameter. If there are no errors, the first parameter will be null or undefined.
Node.js provides multiple methods to delete files. For simple file deletion operations, we can use the fs.unlink() or fs.unlinkSync() function. The former is an asynchronous operation and the latter is a synchronous operation. If you want to delete an empty directory, you can use the fs.rmdir() function. If you want to delete a non-empty directory, you can use the rimraf() function. We should choose the appropriate method to delete files according to the specific scenario.
The above is the detailed content of How to delete files in nodejs. For more information, please follow other related articles on the PHP Chinese website!