Quantcast
Channel: 小惡魔 – 電腦技術 – 工作筆記 – AppleBOY
Viewing all articles
Browse latest Browse all 325

用一行指令加速 npm install

$
0
0
Npm-logo.svg 不久之前寫過一篇 提升 npm install 安裝速度,但是發現速度還是沒有變快,在 DevOps 的流程花在 npm install 的時間非常冗長,造成每次測試或 Deploy 都要花大量時間等待,且吃掉很多機器的資源,本篇要提供一個小技巧改善 npm install 安裝時間,其實簡單來說就是 cache 第一次安裝好的 node_modules 目錄,之後每次安裝就拿 cache 目錄來新增或減少 packages 即可。

一行指令

底下一行指令請加入測試流程內,讓 CI Server 專注在測試
$ tar xf ../nm_cache.tar && \
  npm prune && \
  npm install && \
  tar cf ../nm_cache.tar node_modules
步驟很簡單,先拿上一次備份的 node_modules,再透過 npm prune 移除不必要的 package,再透過 npm install 安裝新的 package,最後一樣打包給下一次測試使用。這指令非常好用,不管你是不是用 npm@3 都很需要這指令加速 npm install。底下是我隨意拿一個 open source 專案來測試,先假設沒有 cache 機制。
$ rm -rf ~/.npm && rm -rf node_modules && time npm install

real    2m7.751s
user    1m8.704s
sys 0m19.272s
如果導入 cache 機制
$ time (tar xf ../nm_cache.tar && npm prune && rm -rf ~/.npm && npm install && tar cf ../nm_cache.tar node_modules)

> labs-web@0.0.1 postinstall /Users/mtk10671/git/labs-web
> node node_modules/fbjs-scripts/node/check-dev-engines.js package.json

real    0m32.370s
user    0m19.884s
sys 0m13.582s
從 2 分 7 秒變成 32 秒,大約提升了 4 倍,大家可以嘗試看看,這招在 Deploy 跟測試非常有感覺。

Viewing all articles
Browse latest Browse all 325

Trending Articles


Vimeo 10.7.1 by Vimeo.com, Inc.


UPDATE SC IDOL: TWO BECOME ONE


KASAMBAHAY BILL IN THE HOUSE


Girasoles para colorear


Presence Quotes – Positive Quotes


EASY COME, EASY GO


Love with Heart Breaking Quotes


Re:Mutton Pies (lleechef)


Ka longiing longsem kaba skhem bad kaba khlain ka pynlong kein ia ka...


Vimeo 10.7.0 by Vimeo.com, Inc.


FORECLOSURE OF REAL ESTATE MORTGAGE


FORTUITOUS EVENT


Pokemon para colorear


Sapos para colorear


Smile Quotes


Letting Go Quotes


Love Song lyrics that marks your Heart


RE: Mutton Pies (frankie241)


Hato lada ym dei namar ka jingpyrshah jong U JJM Nichols Roy (Bah Joy) ngin...


Long Distance Relationship Tagalog Love Quotes