iTranslated by AI
Getting Started with Deno v2.0
Introduction
Deno v2.0 was recently released. Along with it, various articles and posts about Deno have been shared on Zenn, X, and elsewhere, making it a very hot topic right now. Seeing those, I decided to take this opportunity to try out Deno and write this article. As an introductory guide, I will cover installation, project setup, and how to use the Deno CLI.
I will proceed according to the following documentation.
Installation
- Mac
curl -fsSL https://deno.land/install.sh | sh
# or
brew install deno
- Windows
irm https://deno.land/install.ps1 | iex
# or
scoop install deno
# or
choco install deno
# or
winget install DenoLand.Deno
- Linux
curl -fsSL https://deno.land/install.sh | sh
# or
nix-shell -p deno
You can also install using cargo on Mac, Windows, and Linux.
cargo install deno --locked
If you have already installed Deno, you can use the latest version by running the deno upgrade command. (Execution result as of 2024.10.10 👇)
% deno upgrade
Current Deno version: v2.0.0
Looking up stable version
Local deno version 2.0.0 is the most recent release
Project Setup
You can easily set up a Deno project with the following command.
deno init my_project
As a result of execution, a project with the following structure is created.
my_project
├── deno.json
├── main_test.ts
└── main.ts
Let's run the code right away. You can execute the code using deno main.ts or deno run main.ts. Deno natively supports TypeScript, so you can run .ts files as they are. It's quite convenient.
cd my_project
deno main.ts
# Add 2 + 3 = 5
You've successfully run your first program with Deno.
Furthermore, since the project includes a file with test code, let's try running the tests. Tests can be executed with the deno test command.
deno test
running 1 test from ./main_test.ts
addTest ... ok (1ms)
ok | 1 passed | 0 failed (3ms)
This completes the project setup and basic operation check.
Deno CLI
The Deno CLI provides commands and options for various functions, such as executing scripts, managing dependencies, and even compiling code into executable files. All available commands and options can be checked with the deno help or deno -h (--help) command.
Passing Command Line Arguments
When starting a program, you pass command line arguments after the filename.
$ deno main.ts arg1 arg2 arg3
A program that receives and outputs these looks like the following. Command line arguments are handled as an array of strings.
console.log(Deno.args) // ["arg1", "arg2", "arg3"]
Be Careful with the Order of Arguments and Flags
Since everything written after the filename is considered a command line argument, you need to be careful with the order. Make sure that a flag you intended to pass as a command option doesn't end up being treated as a command line argument.
# ⭕️ net_client.ts is executed with the --allow-net option enabled
$ deno run --allow-net net_client.ts
# ❌ --allow-net is passed to Deno.args
$ deno run net_client.ts --allow-net
Commonly Used Flags
Watch mode: --watch
By using deno run --watch main.ts, you can enable automatic reloading of the application whenever changes are detected in the source files. This is particularly useful during development, as you can see the effects of your changes immediately without having to restart the application manually.
It is valid for the deno run, deno test, deno compile, and deno fmt commands. However, the files being monitored differ depending on each command.
-
deno run,deno test,deno compile: The entry point file and all local files it statically imports. -
deno fmt: All local files and directories passed as command line arguments. If no files or directories are specified as arguments, the working directory is targeted.
deno run --watch main.ts
deno test --watch
deno fmt --watch
Personally, I found deno fmt --watch to be very useful. A good way to use it is to have deno fmt --watch running in one terminal while executing other commands like deno run --watch main.ts in another. This allows you to code while keeping your code formatted at regular intervals (the default is likely around 1000ms).
If you want to exclude specific files from monitoring, you can specify them with --watch-exclude.
deno run --watch --watch-exclude=file1.ts,file2.ts main.ts
When specifying using wildcards, you need to enclose the path in double or single quotes.
deno run --watch --watch-exclude='*.js' main.ts
Hot Module Replacement mode: --watch-hmr
You can turn on the HMR feature by adding the --watch-hmr flag to the deno run command. When started in HMR mode, the program doesn't restart when a file change is detected; instead, updates are performed by replacing modules. This is the difference compared to --watch. If the update by replacement fails, the program will restart.
Type Checking: check
You can perform type checking without executing the program.
deno check main.ts
Alternatively, you can run the program after performing type checking.
deno run --check main.ts
While type errors can be checked in your editor, it seems like a useful feature since it becomes difficult to check everything manually as the code grows larger.
Conclusion
Thank you for reading this far.
Personally, even with just the parts covered here, I had several "Aha!" moments realizing that such convenient features are included by default. While Deno has always been considered superior to other runtimes in terms of Node.js compatibility, with the release of v2.0 and further updates, I feel that now is exactly the right time for it to start being widely used.
I plan to keep up with deeper parts of Deno in the future and continue writing articles as a record of my learning, so I would be happy if you read them if you are interested.
Discussion