This article mainly introduces the method of deleting files in nodeJS, and analyzes the operations related to obtaining, traversing, and deleting files and directories in nodeJS by using examples, for more information about how to delete a node. js file, see the following example. We will share this with you for your reference. The details are as follows:
Var fs = require ("fs"); var path = require ("path"); deleteFolderRecursive = function (url) {var files = []; // determine whether the specified path exists if (fs. existsSync (url) {// array files of returned files and subdirectories = fs. readdirSync (url); files. forEach (function (file, index) {// var curPath = url + "/" + file; var curPath = path. join (url, file); // fs. statSync synchronously reads folder files. if it is a folder, trigger the function if (fs. statSync (curPath ). isDirectory () {// recurse deleteFolderRecursive (curPath); // yes file delete file} else {fs. unlinkSync (curPath) ;}}); // clear the folder fs. rmdirSync (url);} else {console. log ("the specified path does not exist, please provide the correct path") ;}}; deleteFolderRecursive (". /node_modules ");
I hope this article will help you design nodejs programs.
For more articles on nodeJS file deletion method examples, refer to PHP Chinese network!