node 脚本删除本地所有node-module
时间: 2023-11-21 20:10:01 浏览: 34
以下是可以删除本地所有node-modules的Node.js脚本:
```javascript
const fs = require('fs');
const path = require('path');
const deleteFolderRecursive = function (folderPath) {
if (fs.existsSync(folderPath)) {
fs.readdirSync(folderPath).forEach((file) => {
const curPath = path.join(folderPath, file);
if (fs.lstatSync(curPath).isDirectory()) {
deleteFolderRecursive(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(folderPath);
}
};
const nodeModulesPath = path.join(process.cwd(), 'node_modules');
deleteFolderRecursive(nodeModulesPath);
console.log('All node_modules folders have been deleted.');
```
这段脚本会在当前目录下找到`node_modules`文件夹并将其删除。请注意,这将删除当前目录以及所有子目录中的所有`node_modules`文件夹,因此请谨慎使用。