前端移動node_modules到其他位置


說明

現在前端開發都是工程化管理, npm、yarn等包管理工具會在源碼目錄下生產node_modules文件夾;
node_modules文件夾很龐大, 文件數量很多, 有時候希望移動到其他位置, 這里提供一種方法;

原理

創建node_modules、package.json和其他的配置文件(如postcss.config.js)的符號鏈接(symbolic link)

批處理腳本

命名為mknm.bat放到path路徑, 在前端源碼根目錄執行mknm

@rem 作用:在傳入的文件夾下創建一個node_modules的link
@rem 用法: 參數: 一個文件夾的完整路徑, 如果忽略使用當前目錄;
@rem 其他:
@rem 2018/11/08 周四 15:32:59.03
@echo off&SetLocal EnableDelayEdexpansion

rem 傳入一個文件夾路徑
if "%1" equ "" (
  "%~0" "%cd%"
  goto :eof
)
echo 當前路徑: %cd%
echo;

rem 1. 傳入一個文件夾的路徑; 2. 在baseDir創建同名的文件夾; 3. 創建link
set dirPath=%~1
set dirName=%~nx1
set baseDir=D:\_node_modules

if not defined dirPath (
  echo 參數為空; 應該傳入路徑
  pause&goto :eof
)
rem 創建文件夾; 最終的文件夾的路徑是newDir
call :creatDir "!dirName!"

mklink /d "!dirPath!\node_modules" "!newDir!\node_modules"
mklink "!newDir!\package.json" "!dirPath!\package.json"
if exist "!dirPath!\postcss.config.js" (
  mklink "!newDir!\postcss.config.js" "!dirPath!\postcss.config.js"
)


set file="!newDir!\readme.txt"
(
echo mklink /d  "!dirPath!\node_modules" "!newDir!\node_modules"

echo mklink "!newDir!\package.json" "!dirPath!\package.json"

echo mklink "!newDir!\postcss.config.js" "!dirPath!\postcss.config.js"

) >%file%


echo 完成..........
goto :eof


:creatDir
set "name=%~1"
:A
set newDir=!baseDir!\!name!
if not exist "!newDir!" (
  mkdir "!newDir!\node_modules"
  goto :eof
) else (
  set "name=!name!-1"
  goto :A
)

:eof

注意

不保證100%有效, 可能install出錯、運行出錯, 可以嘗試把其他文件(比如vue.config.js)也link一下;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM