关于node.js:前端移动nodemodules到其他位置

6次阅读

共计 1284 个字符,预计需要花费 4 分钟才能阅读完成。

[toc]

阐明

当初前端开发都是工程化治理, 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 一下;

正文完
 0