跳至主要內容

deno init,開始一個新專案

使用 Deno 開始一個新專案一直以來都非常簡單:你只需要一個檔案就能開始。不需要任何設定檔、相依性清單或建置指令碼。

來自其他生態系統的使用者通常不習慣這種簡潔性 - 他們常常尋找一個工具來建立一個基本的專案結構,讓他們能夠正確地開始。deno init 子指令建立一個基本的 Deno 專案。

$ deno init
✅ Project initialized
Run these commands to get started

// Run the program
deno run main.ts

// Run the program and watch for file changes
deno task dev

// Run the tests
deno test

// Run the benchmarks
deno bench

$ deno run main.ts
Add 2 + 3 = 5

$ deno test
Check file:///dev/main_test.ts
running 1 test from main_test.ts
addTest ... ok (6ms)

ok | 1 passed | 0 failed (29ms)

這個子指令將會建立兩個檔案(main.tsmain_test.ts)。這些檔案提供了如何撰寫 Deno 程式以及如何為其撰寫測試的基本範例。main.ts 檔案匯出一個 add 函式,用來將兩個數字加總,而 main_test.ts 檔案包含這個函式的測試。

你也可以指定一個參數給 deno init,在特定目錄中初始化一個專案

$ deno init my_deno_project
✅ Project initialized

Run these commands to get started

cd my_deno_project

// Run the program
deno run main.ts

// Run the program and watch for file changes
deno task dev

// Run the tests
deno test

// Run the benchmarks
deno bench