deno.com
在本頁面上

deno.json 和 package.json

您可以使用 deno.json 檔案來設定 Deno。此檔案可用於設定 TypeScript 編譯器、程式碼檢查器、格式化工具和其他 Deno 工具。

組態檔支援 .json.jsonc 副檔名。

如果 deno.jsondeno.jsonc 組態檔位於您目前的工作目錄或父目錄中,Deno 會自動偵測到它。--config 標誌可用於指定不同的組態檔。

package.json 支援 Jump to heading

Deno 也支援 package.json 檔案,以與 Node.js 專案相容。如果您有 Node.js 專案,則無需建立 deno.json 檔案。Deno 將使用 package.json 檔案來設定專案。

如果同一個目錄中同時存在 deno.jsonpackage.json 檔案,Deno 將理解在 deno.jsonpackage.json 中指定的依賴性;並使用 deno.json 檔案進行 Deno 特定的組態。閱讀更多關於 Deno 中的 Node 相容性

依賴性 Jump to heading

deno.json 中的 "imports" 欄位允許您指定專案中使用的依賴性。您可以使用它將裸標識符映射到 URL 或檔案路徑,從而更輕鬆地管理應用程式中的依賴性和模組解析。

例如,如果您想在專案中使用標準函式庫中的 assert 模組,您可以使用此匯入地圖

deno.json
{
  "imports": {
    "@std/assert": "jsr:@std/assert@^1.0.0",
    "chalk": "npm:chalk@5"
  }
}

然後您的腳本可以使用裸標識符 std/assert

script.ts
import { assertEquals } from "@std/assert";
import chalk from "chalk";

assertEquals(1, 2);
console.log(chalk.yellow("Hello world"));

您也可以在 package.json 中使用 "dependencies" 欄位

package.json
{
  "dependencies": {
    "express": "express@^1.0.0"
  }
}
script.ts
import express from "express";

const app = express();

請注意,這會要求您執行 deno install

閱讀更多關於 模組匯入和依賴性

自訂路徑映射 Jump to heading

deno.json 中的匯入地圖可用於更通用的標識符路徑映射。您可以將確切的標識符映射到第三方模組或直接映射到檔案,或者您可以將匯入標識符的一部分映射到目錄。

deno.jsonc
{
  "imports": {
    // Map to an exact file
    "foo": "./some/long/path/foo.ts",
    // Map to a directory, usage: "bar/file.ts"
    "bar/": "./some/folder/bar/"
  }
}

用法

import * as foo from "foo";
import * as bar from "bar/file.ts";

匯入指定的路徑映射通常用於較大的程式碼庫中,以求簡潔。

若要將您的專案根目錄用於絕對匯入

deno.json
{
  "imports": {
    "/": "./",
    "./": "./"
  }
}
main.ts
import { MyUtil } from "/util.ts";

這會導致以 / 開頭的匯入標識符相對於匯入地圖的 URL 或檔案路徑進行解析。

工作 Jump to heading

deno.json 檔案中的 tasks 欄位用於定義可以使用 deno task 命令執行的自訂命令,並允許您根據專案的特定需求調整命令和權限。

它類似於 package.json 檔案中的 scripts 欄位,後者也受到支援。

deno.json
{
  "tasks": {
    "start": "deno run --allow-net --watch=static/,routes/,data/ dev.ts",
    "test": "deno test --allow-net",
    "lint": "deno lint"
  }
}
package.json
{
  "scripts": {
    "dev": "vite dev",
    "build": "vite build"
  }
}

若要執行工作,請使用 deno task 命令,後跟工作名稱。例如

deno task start
deno task test
deno task lint
deno task dev
deno task build

閱讀更多關於 deno task 的資訊。

程式碼檢查 Jump to heading

deno.json 檔案中的 lint 欄位用於設定 Deno 內建程式碼檢查器的行為。這允許您指定要包含或排除在程式碼檢查之外的檔案,以及自訂程式碼檢查規則以符合您專案的需求。

例如

deno.json
{
  "lint": {
    "include": ["src/"],
    "exclude": ["src/testdata/", "src/fixtures/**/*.ts"],
    "rules": {
      "tags": ["recommended"],
      "include": ["ban-untagged-todo"],
      "exclude": ["no-unused-vars"]
    }
  }
}

此設定將

  • 僅檢查 src/ 目錄中的檔案,
  • 不會檢查 src/testdata/ 目錄或 src/fixtures/ 目錄中的任何 TypeScript 檔案。
  • 指定應套用建議的程式碼檢查規則,
  • 新增 ban-untagged-todo
  • 移除排除的 no-unused-vars 規則。

您可以在「規則列表」文件頁面中找到可用程式碼檢查規則的完整列表。

閱讀更多關於 使用 Deno 進行程式碼檢查 的資訊。

格式化 Jump to heading

deno.json 檔案中的 fmt 欄位用於設定 Deno 內建程式碼格式化工具的行為。這允許您自訂程式碼的格式化方式,確保專案中的一致性,使其更易於閱讀和協作。以下是您可以設定的關鍵選項

deno.json
{
  "fmt": {
    "useTabs": true,
    "lineWidth": 80,
    "indentWidth": 4,
    "semiColons": true,
    "singleQuote": true,
    "proseWrap": "preserve",
    "include": ["src/"],
    "exclude": ["src/testdata/", "src/fixtures/**/*.ts"]
  }
}

此設定將

  • 使用 Tab 字元而不是空格進行縮排,
  • 將行寬限制為 80 個字元,
  • 使用 4 個空格的縮排寬度,
  • 在語句末尾新增分號,
  • 字串使用單引號,
  • 保留散文換行,
  • 格式化 src/ 目錄中的檔案,
  • 排除 src/testdata/ 目錄中的檔案以及 src/fixtures/ 目錄中的任何 TypeScript 檔案。

閱讀更多關於 使用 Deno 格式化程式碼 的資訊。

鎖定檔 Jump to heading

deno.json 檔案中的 lock 欄位用於指定鎖定檔的組態,Deno 使用鎖定檔來確保依賴性的完整性。鎖定檔記錄專案依賴模組的確切版本和完整性雜湊值,確保每次執行專案時都使用相同的版本,即使依賴性已更新或遠端變更。

deno.json
{
  "lock": {
    "path": "./deno.lock",
    "frozen": true
  }
}

此設定將

  • 指定鎖定檔位置在 ./deno.lock (這是預設值,可以省略)
  • 告知 Deno,如果任何依賴性變更,您想要產生錯誤

Deno 預設使用鎖定檔,您可以使用以下組態停用它

deno.json
{
  "lock": false
}

Node modules 目錄 Jump to heading

預設情況下,如果您的專案目錄中有 package.json 檔案,Deno 會使用本地 node_modules 目錄。

您可以使用 deno.json 檔案中的 nodeModulesDir 欄位來控制此行為。

deno.json
{
  "nodeModulesDir": "auto"
}

您可以將此欄位設定為以下值

行為
"none" 不要使用本地 node_modules 目錄。而是使用 $DENO_DIR 中的全域快取,Deno 會自動保持更新。
"auto" 使用本地 node_modules 目錄。該目錄由 Deno 自動建立並保持更新。
"manual" 使用本地 node_modules 目錄。使用者必須手動保持此目錄更新,例如使用 deno installnpm install

不需要指定此設定,將套用以下預設值

  • 如果您的專案目錄中沒有 package.json 檔案,則為 "none"
  • 如果您的專案目錄中有 package.json 檔案,則為 "manual"

使用工作區時,此設定只能在工作區根目錄中使用。在任何成員中指定它都會導致警告。只有在工作區根目錄中有 package.json 檔案時,才會自動套用 "manual" 設定。

TypeScript 編譯器選項 Jump to heading

deno.json 檔案中的 compilerOptions 欄位用於設定 Deno 專案的 TypeScript 編譯器設定。這允許您自訂 TypeScript 程式碼的編譯方式,確保其符合您專案的需求和編碼標準。

資訊

Deno 建議預設的 TypeScript 組態。這將有助於共享程式碼。

另請參閱在 Deno 中設定 TypeScript

不穩定功能 Jump to heading

deno.json 檔案中的 unstable 欄位用於為您的 Deno 專案啟用特定的不穩定功能。

這些功能仍在開發中,尚未成為穩定 API 的一部分。透過在 unstable 陣列中列出功能,您可以在這些新功能正式發布之前試用和使用它們。

deno.json
{
  "unstable": ["cron", "kv", "webgpu"]
}

了解更多.

包含與排除 Jump to heading

許多組態 (例如 lintfmt) 都有 includeexclude 屬性,用於指定要包含的檔案。

包含 Jump to heading

僅包含在此處指定的路徑或模式。

{
  "lint": {
    // only format the src/ directory
    "include": ["src/"]
  }
}

排除 Jump to heading

將排除在此處指定的路徑或模式。

{
  "lint": {
    // don't lint the dist/ folder
    "exclude": ["dist/"]
  }
}

這比 include 具有更高的優先順序,如果路徑在 includeexclude 中都匹配,則將優先於 include

您可能希望排除目錄,但包含子目錄。在 Deno 1.41.2+ 中,您可以透過在更一般的排除下方指定否定 glob 來取消排除更特定的路徑

{
  "fmt": {
    // don't format the "fixtures" directory,
    // but do format "fixtures/scripts"
    "exclude": [
      "fixtures",
      "!fixtures/scripts"
    ]
  }
}

頂層排除 Jump to heading

如果有您永遠不希望 Deno 格式化、檢查、類型檢查、在 LSP 中分析等的目錄,請在頂層排除陣列中指定它

{
  "exclude": [
    // exclude the dist folder from all sub-commands and the LSP
    "dist/"
  ]
}

有時您可能會發現您想要取消排除在頂層排除中排除的路徑或模式。在 Deno 1.41.2+ 中,您可以透過在更具體的組態中指定否定 glob 來取消排除路徑

{
  "fmt": {
    "exclude": [
      // format the dist folder even though it's
      // excluded at the top level
      "!dist"
    ]
  },
  "exclude": [
    "dist/"
  ]
}

發布 - 覆寫 .gitignore Jump to heading

.gitignore 用於 deno publish 命令。在 Deno 1.41.2+ 中,您可以選擇不排除 .gitignore 中忽略的檔案,方法是使用否定排除 glob

.gitignore
dist/
.env
deno.json
{
  "publish": {
    "exclude": [
      // include the .gitignored dist folder
      "!dist/"
    ]
  }
}

或者,在 "include" 中明確指定被 .gitignore 忽略的路徑也有效

{
  "publish": {
    "include": [
      "dist/",
      "README.md",
      "deno.json"
    ]
  }
}

完整範例 Jump to heading

{
  "compilerOptions": {
    "allowJs": true,
    "lib": ["deno.window"],
    "strict": true
  },
  "lint": {
    "include": ["src/"],
    "exclude": ["src/testdata/", "src/fixtures/**/*.ts"],
    "rules": {
      "tags": ["recommended"],
      "include": ["ban-untagged-todo"],
      "exclude": ["no-unused-vars"]
    }
  },
  "fmt": {
    "useTabs": true,
    "lineWidth": 80,
    "indentWidth": 4,
    "semiColons": false,
    "singleQuote": true,
    "proseWrap": "preserve",
    "include": ["src/"],
    "exclude": ["src/testdata/", "src/fixtures/**/*.ts"]
  },
  "lock": false,
  "nodeModulesDir": "auto",
  "unstable": ["webgpu"],
  "test": {
    "include": ["src/"],
    "exclude": ["src/testdata/", "src/fixtures/**/*.ts"]
  },
  "tasks": {
    "start": "deno run --allow-read main.ts"
  },
  "imports": {
    "oak": "jsr:@oak/oak"
  },
  "exclude": [
    "dist/"
  ]
}

JSON Schema Jump to heading

JSON Schema 檔案可用於編輯器以提供自動完成功能。該檔案已版本化,可在以下網址取得: https://deno.land/x/deno/cli/schemas/config-file.v1.json

代理伺服器 Jump to heading

Deno 支援用於模組下載和 fetch API 的代理伺服器。代理伺服器組態是從環境變數讀取的:HTTP_PROXY、HTTPS_PROXY 和 NO_PROXY。

如果您使用的是 Windows - 如果找不到環境變數,Deno 會退回到從登錄檔讀取代理伺服器。

您找到需要的資訊了嗎?

隱私權政策