6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Nodejsの上位代替?Denoを使ってみた

Posted at

最近、SNS上にdenoの話をよく目にしました。https://deno.com/

Deno,the next generation JavaScript runtimeとのスローガンを掲げ、一体どのような感じなので、実際試してみました。驚くなど使いやすかったです。js、tsと比べて、いつくメリットを感じたことについて共有します。

  • TypeScriptのネイティブサポート

javascriptにコンパイルしないと、今までではtypescriptファイルを直接実行することはできなかったが、denoでは直接にtsファイルを実行できます。

deno-test % deno run main.ts 
> Add 2 + 3 = 5
  • ビルトインツールとすぐに使える機能

Denoはコードフォーマット(deno fmt)、依存関係チェッカー(deno info)、テストフレームワークなどの開発ツールを内蔵しており、追加の設定なしで利用できます。nodejs時代にサードパーティのライブラリーをたくさんインストールしなくても済みます。

// jestなどのUTライブラリーはいらない、UTツールはすでに内蔵している
Deno.test(function addTest() {
  assertEquals(add(2, 3), 5);
});
  • モダンなモジュールシステム

DenoはESモジュール標準(ESM)を採用しており、URLから直接モジュールをインポートできます。(例:import { serve } from "https://deno.land/std/http/server.ts")これにより、Node.jsのnode_modulespackage.jsonに依存しないモジュール管理が可能です。ちなみに、denoは開発者のために、パッケージ共有プラットフォームを用意しています。https://jsr.io/

  • セキュリティ

Denoはデフォルトでサンドボックス環境でコードを実行し、ファイル、ネットワーク、環境変数へのアクセスはすべて明示的な許可が必要です。これにより、悪意のあるコードが許可なく操作することを防ぎます。(もちろん、明示的に権限を付与することも可能)

// main.ts
// localのcsvファイルを読み込み
const reader = await Deno.readTextFile("./test.csv");
console.log(reader);
// deno-test % deno run main.ts
> ⚠️  Deno requests read access to "/Users/Desktop/deno-test/test.csv".
┠─ Requested by `Deno.readFile()` API.
┠─ To see a stack trace for this prompt, set the DENO_TRACE_PERMISSIONS environmental variable.
┠─ Learn more at: https://docs.deno.com/go/--allow-read
┠─ Run again with --allow-read to bypass this prompt.
 Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all read permissions) > y

 Granted read access to "/Users/Desktop/deno-test/test.csv".
id,name
1,userA
2,userB

以上で、denoを試しに使っていました。パフォーマンス、使いやすさ、セキュリティなどの面では、ほぼnodejsより上ですね。ただ、エコシステムでは、denoはまだまだ成長中で、規模はnodejsほど大きくないです。また、DenoはNode.jsのAPIの一部を互換性を持たせており、npmパッケージのサポートも進めていますが、まだ少し時間かかりそうです。今後の展開を楽しみしております。皆さんも試してみてはいかがでしょうか?

6
0
1

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
6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?