Skip to content

Instantly share code, notes, and snippets.

@patheticGeek
Last active January 10, 2023 20:05
Show Gist options
  • Save patheticGeek/1bceeabb3d5c7a70da2fcdb1360a5e47 to your computer and use it in GitHub Desktop.
Save patheticGeek/1bceeabb3d5c7a70da2fcdb1360a5e47 to your computer and use it in GitHub Desktop.
Deletes all node_modules folder in the current dir tree
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