LoginSignup
49
33

More than 3 years have passed since last update.

Deno触ってみた

Last updated at Posted at 2020-05-12

はじめに

Qiitaのトレンドをいつものように眺めていたらこちらの記事
Denoの登場でNode.jsの時代は終わるのか?
を見つけて興味を持ってサンプルコードを動かしてみたログです。

Node.jsにいつか置き換わるかもしれないJS/TSランタイムだそうです。

Deno公式

ほんとうに入門の入門のところですが、チュートリアルで誰もがつまづきそうなポイントがあったので記事にしました。

【追記】
このような記事も見つけました。いろいろな違いがありますね。
DenoとNode.jsの大きな違い

インストール

インストール方法は公式サイト参照

Windows 10へのインストール手順

私はWindows10ユーザーなので、Windows10へのインストール方法を紹介します。
Windows10にDenoをインストールする場合、Chocolateyを使ってインストールをするので、まずはChocolateyを解説します。

0.Chocolateyをインストール

Chocolateyとは、はこちら参照ー>Chocolateyを使った環境構築の時のメモ

(公式のInstalling Chocolateyを見ながら)

1.PowerShellを管理者権限で実行(CMDじゃなくて、PowerShell)
2.以下のコマンドをコピペして実行

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Run Get-ExecutionPolicy. If it returns Restricted, then run Set-ExecutionPolicy AllSigned or Set-ExecutionPolicy Bypass -Scope Process.

PowerShell上で Get-ExecutionPolicyを実行して Restrictedと表示された場合は、上記コマンドの先頭を Set-ExecutionPolicy AllSigned にすればOKです。

> Get-ExecutionPolicy
Restricted

3.インストール完了まで少し待つ
chocoコマンドが実行できればOKです。

> choco
Chocolatey v0.10.15
Please run 'choco -?' or 'choco <command> -?' for help menu.

Denoをインストール

続いてDenoをインストールします。
1.コマンドプロンプトを管理者権限で実行
2.以下のコマンドを実行

choco install deno

3.サンプルコードを動かして動作確認

>deno run https://deno.land/std/examples/welcome.ts
Welcome to Deno 🦕

コマンドプロンプト上で実行したときには"Deno"のあとに点が表示されるだけでした。
Qiitaにコピペして気づきましたが、これは恐竜の絵文字だったらしいです。
image.png

サンプルコードを動かす

Deno公式にあったOr a more complex one:のサンプルコードを動かしてみます

sample.ts
import { serve } from "https://deno.land/std@0.50.0/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
  req.respond({ body: "Hello World\n" });
}
> deno sample.ts
Compile file:///C:/study/deno/sample.ts
Download https://deno.land/std@0.50.0/http/server.ts
Download https://deno.land/std@0.50.0/encoding/utf8.ts  
Download https://deno.land/std@0.50.0/io/bufio.ts       
Download https://deno.land/std@0.50.0/testing/asserts.ts
Download https://deno.land/std@0.50.0/async/mod.ts      
Download https://deno.land/std@0.50.0/http/_io.ts       
Download https://deno.land/std@0.50.0/io/util.ts
Download https://deno.land/std@0.50.0/path/mod.ts
Download https://deno.land/std@0.50.0/path/win32.ts
Download https://deno.land/std@0.50.0/path/posix.ts
Download https://deno.land/std@0.50.0/path/common.ts
Download https://deno.land/std@0.50.0/path/separator.ts
Download https://deno.land/std@0.50.0/path/interface.ts
Download https://deno.land/std@0.50.0/path/glob.ts
Download https://deno.land/std@0.50.0/path/_constants.ts
Download https://deno.land/std@0.50.0/path/_util.ts
Download https://deno.land/std@0.50.0/fmt/colors.ts
Download https://deno.land/std@0.50.0/testing/diff.ts
Download https://deno.land/std@0.50.0/path/_globrex.ts
Download https://deno.land/std@0.50.0/async/deferred.ts
Download https://deno.land/std@0.50.0/async/delay.ts
Download https://deno.land/std@0.50.0/async/mux_async_iterator.ts
Download https://deno.land/std@0.50.0/textproto/mod.ts
Download https://deno.land/std@0.50.0/http/http_status.ts
Download https://deno.land/std@0.50.0/bytes/mod.ts
error: Uncaught PermissionDenied: network access to "127.0.0.1:8000", run again with the --allow-net flag
    at unwrapResponse ($deno$/ops/dispatch_json.ts:43:11)
    at Object.sendSync ($deno$/ops/dispatch_json.ts:72:10)
    at Object.listen ($deno$/ops/net.ts:51:10)
    at listen ($deno$/net.ts:164:18)
    at serve (server.ts:261:20)
    at sample.ts:2:11

エラーが起きます。
error: Uncaught PermissionDenied:

エラーメッセージにもある通り --allow-net のフラグをつけないとネットワークアクセスができないです。

これはDenoの登場でNode.jsの時代は終わるのか?のこちらの記載のとおりですね。

Denoはdefaultで安全です(許可なしではファイル・ネットワーク・環境にアクセスできません)

記事を読んだときにはどうやって許可するんだろう?と思っていましたが、フラグを付けて実行をしないといけないということだったんですね。

フラグは denoのあとに付ければいいです。
sample.tsのあとに付けても error: Uncaught PermissionDenied: となります。

> deno run --allow-net sample.ts
http://localhost:8000/

runをつけなくても実行はできますが、公式のGetting Startedを見ると run付きで実行しているので、付けた方が良さそう。

deno --allow-net sample.ts
http://localhost:8000/

よく使いそうなフラグ

Network Access: --allow-net
Environment Variables: --allow-env
Read filesystem: --allow-read
Write filesystem: --allow-write

他にも --allow-hrtime みたいのがあるそう

参考:
error: Uncaught PermissionDenied: network access to "127.0.0.1:8000", run again with the --allow-net flag

おわりに

やっぱりNode.jsよりコードは書きやすそうな印象です。いつか案件で使ってみたい。

参考

49
33
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
49
33