Vue多種高效刪除node_modules的方法
刪除 node_modules 慢是常見(jiàn)問(wèn)題,這里有幾種高效方法:
1.使用專(zhuān)用刪除工具(推薦)
rimraf(跨平臺(tái))
# 全局安裝 npm install -g rimraf # 在項(xiàng)目目錄執(zhí)行 rimraf node_modules # 或使用npx(無(wú)需全局安裝) 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自動(dòng)清理 pnpm store prune # 刪除node_modules pnpm dlx rimraf node_modules
Yarn
# Yarn 2+ 有自動(dòng)清理 yarn cache clean
4.使用腳本/自動(dòng)化
創(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 # 使用符號(hào)鏈接(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)建一個(gè)空目錄,然后用它替換node_modules mkdir empty_dir rsync -a --delete empty_dir/ node_modules/ rmdir empty_dir node_modules
最佳實(shí)踐建議
- 定期清理:不要等到
node_modules非常大時(shí)才刪除 - 使用.gitignore:確保不會(huì)誤提交到版本控制
- 按需安裝:使用
npm ci --only=production只安裝生產(chǎn)依賴(lài) - 考慮使用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)原生命令,它們通常比手動(dòng)刪除快10倍以上。對(duì)于超大型項(xiàng)目,可以考慮使用專(zhuān)門(mén)的清理工具或優(yōu)化項(xiàng)目結(jié)構(gòu)。
到此這篇關(guān)于Vue多種搞笑刪除node_modules的方法的文章就介紹到這了,更多相關(guān)vue刪除 node_modules內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 完美解決vue 項(xiàng)目開(kāi)發(fā)越久 node_modules包越大的問(wèn)題
- vue導(dǎo)入新工程?“node_modules依賴(lài)”問(wèn)題
- 多個(gè)vue項(xiàng)目復(fù)用一個(gè)node_modules的問(wèn)題
- vue上傳項(xiàng)目到git時(shí),如何忽略node_modules文件夾
- 如何刪除vue項(xiàng)目下的node_modules文件夾
- 多個(gè)vue項(xiàng)目實(shí)現(xiàn)共用一個(gè)node-modules文件夾
- vue中node_modules中第三方模塊的修改使用詳解
相關(guān)文章
基于vue框架手寫(xiě)一個(gè)notify插件實(shí)現(xiàn)通知功能的方法
這篇文章主要介紹了基于vue框架手寫(xiě)一個(gè)notify插件實(shí)現(xiàn)通知功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Vue 使用iframe引用html頁(yè)面實(shí)現(xiàn)vue和html頁(yè)面方法的調(diào)用操作
這篇文章主要介紹了Vue 使用iframe引用html頁(yè)面實(shí)現(xiàn)vue和html頁(yè)面方法的調(diào)用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Vue實(shí)現(xiàn)登錄功能全套超詳細(xì)講解(含封裝axios)
這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)登錄功能(含封裝axios)的相關(guān)資料,Vue是現(xiàn)在前端最流行的框架之一,作為前端開(kāi)發(fā)人員應(yīng)該要熟練的掌握它,需要的朋友可以參考下2023-10-10
Vue結(jié)合原生js實(shí)現(xiàn)自定義組件自動(dòng)生成示例
VUE使用router.push實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)和傳參方式

