Home >Web Front-end >JS Tutorial >How to delete files in nodejs fs
Nodejs fs method to delete files: 1. Create a new JS file and import the fs module; 2. Use the mkdirSync method to create the folder user; 3. Call the open() method in the fs module to open the file; 4. Call The unlink method can delete the file.
The operating environment of this article: windows7 system, nodejs version 10.16.2, Dell G3 computer.
How to delete files in nodejs fs?
How to delete files in the core module FS in nodejs:
Double-click to open the HBuilderX tool and create a new Web project, which contains related templates
Right-click on the js folder to create a new JavaScript file, enter the file name and click Create
to open the new JS file , import the fs module, and use the mkdirSync method to create the folder user, and then call the writeFile method to write the content to the file
Then, call open() in the fs module Method, open the newly created file
Since the HBuilderX tool does not directly run the file corresponding to Node.js, copy the file to the HBuilder tool, and then run it directly
Open the js folder, you can find that the user folder is generated in the directory, and the data.txt file is generated in the folder
Click and Open data.txt, you can find that the content is written in this file
Finally, in the js file, call the unlink method, delete the file data.txt, and then check whether the file is still there Existence
Recommended learning: "node.js video tutorial"
The above is the detailed content of How to delete files in nodejs fs. For more information, please follow other related articles on the PHP Chinese website!