I am trying to remove the node_modules directory if it exists and is not empty
var fs = require('fs'), path = require('path'), child_process = require('child_process'), cmd; module.exports = function(){ var modulesPath = '../modules'; fs.readdirSync(modulesPath) .forEach(function(dir) { var location = path.join(dir, 'node_modules'); if (fs.existsSync(location)){ fs.rmdir(location); } }); };
The fs.rmdir command, unfortunately, only removes a directory if there are no files there. NodeJS has no easy way to force removal.
source share