Last active
January 10, 2023 20:05
-
-
Save patheticGeek/1bceeabb3d5c7a70da2fcdb1360a5e47 to your computer and use it in GitHub Desktop.
Deletes all node_modules folder in the current dir tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
function deleteNodeModules(path) { | |
try { | |
const contents = fs.readdirSync(path, { withFileTypes: true }); | |
contents.forEach(val => { | |
const name = val.name; | |
const isDir = val.isDirectory(); | |
const isFile = val.isFile(); | |
if (isDir && !name.startsWith('.')) { | |
if(name === 'node_modules') { | |
console.log(`Deleting: ${path}/${name}`); | |
fs.rmdirSync(`${path}/${name}`, {recursive: true}); | |
} | |
else deleteNodeModules(`${path}/${name}`); | |
} | |
}) | |
} catch(e) { | |
console.log(`Error at path ${path}: ${e.message}`); | |
} | |
} | |
deleteNodeModules('.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment