Skip to content

Deno チートシート

ツール群を内蔵した、セキュアな JavaScript/TypeScript ランタイム。

01

はじめに

Hello World

Deno は TS/JS を直接実行できます — コンパイルは不要です。

deno
console.log("Hello, World!");

実行

権限は明示的に指定します: --allow-net、--allow-read など。

deno
# deno run main.ts
# deno run --allow-net server.ts

インポート

Deno は URL インポートを使い、node_modules がありません。

deno
// URL imports
import { serve } from "https://deno.land/std/http/server.ts";

// Local imports
import { helper } from "./utils.ts";
02

標準 API

ファイル I/O

システム API には Deno 名前空間を使います。

deno
const data = await Deno.readTextFile("file.txt");
console.log(data);

await Deno.writeTextFile("out.txt", "content");

HTTP サーバー

非同期イテレータで HTTP サーバーを構築します。

deno
import { serve } from "https://deno.land/std/http/server.ts";

for await (const req of serve(":8000")) {
  req.respond({ body: "Hello!" });
}

fetch

fetch は組み込みで、ポリフィルは不要です。

deno
const res = await fetch("https://api.example.com");
const data = await res.json();
console.log(data);
03

ツール

テスト

テストランナーが組み込まれています。

deno
// deno test
Deno.test("add", () => {
  assertEquals(1 + 1, 2);
});

フォーマット

フォーマッターとリンターが組み込まれています。

deno
# deno fmt main.ts
# deno lint main.ts

Was this helpful?