Vue多種高效刪除node_modules的方法
刪除 node_modules 慢是常見問題,這里有幾種高效方法:
1.使用專用刪除工具(推薦)
rimraf(跨平臺)
# 全局安裝 npm install -g rimraf # 在項(xiàng)目目錄執(zhí)行 rimraf node_modules # 或使用npx(無需全局安裝) npx rimraf node_modules
快速刪除工具
# 1. del-cli npx del-cli node_modules # 2. trash-cli (macOS/Linux) npm install -g trash-cli trash node_modules
2.使用系統(tǒng)命令
Windows
# 使用rd命令(最快) rmdir /s /q node_modules # 或PowerShell Remove-Item -Recurse -Force node_modules
macOS/Linux
# 使用rm命令 rm -rf node_modules # 如果需要sudo權(quán)限 sudo rm -rf node_modules
3.使用包管理器的功能
PNPM
# pnpm自動清理 pnpm store prune # 刪除node_modules pnpm dlx rimraf node_modules
Yarn
# Yarn 2+ 有自動清理 yarn cache clean
4.使用腳本/自動化
創(chuàng)建刪除腳本
delete-nm.js:
const fs = require('fs');
const path = require('path');
function deleteNodeModules(dir) {
if (fs.existsSync(dir)) {
fs.readdirSync(dir).forEach(file => {
const curPath = path.join(dir, file);
if (fs.lstatSync(curPath).isDirectory()) {
deleteNodeModules(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(dir);
}
}
deleteNodeModules('./node_modules');5.預(yù)防和優(yōu)化
使用.npmrc配置
# 防止生成package-lock副本 package-lock=false # 使用符號鏈接(Windows) node-linker=hoisted
使用Docker容器
# 在Docker中操作 docker run --rm -v "$(pwd):/app" node:alpine sh -c "cd /app && rm -rf node_modules"
使用工作區(qū)(Monorepo)
{
"workspaces": ["packages/*"],
"scripts": {
"clean": "lerna clean -y"
}
}6.進(jìn)階技巧
并行刪除(Linux/macOS)
# 使用find并行刪除
find . -name "node_modules" -type d -prune -exec rm -rf '{}' +使用rsync(空目錄替換)
# 創(chuàng)建一個空目錄,然后用它替換node_modules mkdir empty_dir rsync -a --delete empty_dir/ node_modules/ rmdir empty_dir node_modules
最佳實(shí)踐建議
- 定期清理:不要等到
node_modules非常大時才刪除 - 使用.gitignore:確保不會誤提交到版本控制
- 按需安裝:使用
npm ci --only=production只安裝生產(chǎn)依賴 - 考慮使用pnp模式:Yarn 2+ 的 Plug’n’Play 可以避免生成node_modules
一鍵清理腳本
創(chuàng)建 cleanup.sh:
#!/bin/bash
echo "正在清理node_modules..."
find . -name "node_modules" -type d -prune | xargs -I {} sh -c 'echo "刪除 {}" && rm -rf {}'
echo "清理完成!"總結(jié):推薦使用 rimraf 或系統(tǒng)原生命令,它們通常比手動刪除快10倍以上。對于超大型項(xiàng)目,可以考慮使用專門的清理工具或優(yōu)化項(xiàng)目結(jié)構(gòu)。
到此這篇關(guān)于Vue多種搞笑刪除node_modules的方法的文章就介紹到這了,更多相關(guān)vue刪除 node_modules內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于vue框架手寫一個notify插件實(shí)現(xiàn)通知功能的方法
這篇文章主要介紹了基于vue框架手寫一個notify插件實(shí)現(xiàn)通知功能的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Vue 使用iframe引用html頁面實(shí)現(xiàn)vue和html頁面方法的調(diào)用操作
這篇文章主要介紹了Vue 使用iframe引用html頁面實(shí)現(xiàn)vue和html頁面方法的調(diào)用操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue實(shí)現(xiàn)登錄功能全套超詳細(xì)講解(含封裝axios)
這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)登錄功能(含封裝axios)的相關(guān)資料,Vue是現(xiàn)在前端最流行的框架之一,作為前端開發(fā)人員應(yīng)該要熟練的掌握它,需要的朋友可以參考下2023-10-10
Vue結(jié)合原生js實(shí)現(xiàn)自定義組件自動生成示例
VUE使用router.push實(shí)現(xiàn)頁面跳轉(zhuǎn)和傳參方式

