🚀 使用 TypeScript 開發 Node.js 專案的基本檔案結構簡介
Copyright Notice: This article is an original work licensed under the CC 4.0 BY-NC-ND license.
If you wish to repost this article, please include the original source link and this copyright notice.
Source link: https://v2know.com/article/1173
整個 Node.js + TypeScript 專案確實可以概括為 三個主要部分:
✅ 1. src/
:原始碼主目錄
-
開發時編輯的 TypeScript 原始碼。
-
包含:
-
index.ts
(進入點) -
routes/
,controllers/
,services/
,models/
,utils/
等邏輯結構。
-
-
結構清晰、可維護。
✅ 2. dist/
:編譯後的 JavaScript
-
使用
tsc
(TypeScript 編譯器)將src/
轉換成純 JS。 -
不應手動修改,通常會在
.gitignore
忽略它。 -
用於實際執行(如部署到伺服器時)。
✅ 3. 專案根目錄的設定與元資料(JSON + others)
這些檔案控制整個專案行為與依賴:
檔案 | 作用說明 |
---|---|
package.json |
npm 套件設定、scripts 指令、專案名稱與版本等 |
package-lock.json |
鎖定依賴版本,確保可重現安裝環境 |
tsconfig.json |
TypeScript 的編譯規則設定 |
.env |
儲存環境變數(需搭配 dotenv 使用) |
.gitignore |
設定哪些檔案不提交 Git(如 node_modules/ 、dist/ ) |
README.md |
專案說明文件 |
🔁 三者關係簡圖
↓ tsc 編譯
src/ ─────────────→ dist/
↑ ↑
│ │
└──由根目錄設定檔(tsconfig.json, package.json 等)控制行為
🧠 總結一句話:
src
是你寫的,dist
是機器轉出來跑的,根目錄放的 JSON 是讓一切有秩序地運作起來。
→返回目錄
This article was last edited at