跳轉到

6.1 最常使用的 12 個指令#

日常開發 90% 的時間,你只會用到這 12 個指令:

# 指令 用途
1 node app.js 執行程式
2 node --watch app.js 改檔自動重啟(開發模式)
3 npm init -y 初始化專案,生成 package.json
4 npm install 依 package.json 安裝全部依賴(clone 後必跑)
5 npm install <pkg> 安裝套件並加入 dependencies
6 npm install -D <pkg> 安裝開發工具(devDependencies)
7 npm run dev 執行自訂腳本
8 npm test 跑測試
9 node --test 跑測試(Node 內建,不用裝套件)
10 npx <tool> 不用安裝直接執行工具
11 npm outdated 查看哪些套件有新版
12 npm uninstall <pkg> 移除套件

按場景記憶#

開始新專案:

mkdir my-project && cd my-project
npm init -y
npm install express

Clone 別人的專案:

git clone <URL> && cd <專案>
nvm use            # 切換到 .nvmrc 指定的版本
npm install        # 安裝依賴
cp .env.example .env   # 填入自己的設定
npm run dev        # 開跑

日常開發節奏:

npm run test:watch   # 背景跑測試
npm run lint         # commit 前檢查
git add . && git commit -m "說明"

除錯:

node --inspect-brk app.js   # 正式除錯器
node --test                 # 快速驗證邏輯

把這一頁存成書籤,前幾週照著打,兩週後就會內化成肌肉記憶。