First Program with Deno

Learn to start coding with Deno.

Overview

Now that we’re familiar with event-driven languages, Node’s history, and the reasons that led to Deno, we’re in good shape to start writing some code.

We’ll proceed by writing our first Deno program and using the REPL to experiment with the runtime APIs. Then, we’ll explore the module system and how Deno cache and module resolution work with practical examples.

We’ll understand versioning, and we’ll also learn how to handle third-party dependencies. Then, we’ll use the command-line interface (CLI) to explore packages and their documentation as well as how to install and reuse Deno scripts. After running and installing a few scripts, we’ll dive into permissions by learning how the permission system works and how we can secure the code we run.

On our journey of learning about the toolchain, we can’t leave code formatting and linting out, so we’ll also explore these topics in this chapter. We’ll explore Deno’s test suite by writing and running some simple tests, and we’ll finish by presenting how Deno can bundle code into a self-sustainable binary or a single JavaScript file.

In this chapter, we’ll cover the following topics:

  • First Hello World program

  • The module system and third-party dependencies

  • Running and installing scripts

  • Using the test command

  • Using permissions

  • Formatting and linting code

  • Bundling code

  • Compiling to a binary

  • Using the upgrade command

Let’s get started!

Note: The instructions for setting up the environment and installing Deno on a local machine are provided in the appendix.

Hello World program

With everything in place, let's write our first program!

First, we need to create a file named my-first-deno-program.js and write something we’re familiar with. We’ll use the console API to write a message to the console:

Press + to interact
console.log('Hello from deno');

Let’s use the CLI to execute the code. The command we must use to execute programs is called run:

$ deno run my-first-deno-program.js

The output of the program will be:

Hello from deno

Note: All Deno CLI commands can be executed with the --help flag, which will detail all the command’s possible behaviors.

At this point, we haven't done anything that we didn’t know what to do already. We just wrote a console.log file in the language that we're familiar with, JavaScript.

The interesting thing is that we learned how to execute programs using the run command. We’ll explore this in more detail later.

REPL tool

The Read Eval Print Loop, also known as the REPL, is a tool that is commonly used in interpreted languages. It allows users to run lines of code and get their immediate output. Node.js, Ruby, and Python are a few examples of languages where it is heavily used. Deno is no exception.

To open it, we need to run the following command:

$ deno
Command to get the immediate output

We can now spend some time exploring the language. To know what APIs are available, we are in the right place to try them out. We’ll do a deep dive into those later, but to give a few suggestions, we can look at the Deno namespace, Web API-compatible functions, such as fetch, or objects, such as Math or window, all of which are listed in Deno’s documentation.

The eval command

Another way to execute code that doesn't live in a file is by using the eval command:

deno eval "console.log('Hello from eval')"

The output will be:

Hello from eval
Output of Hello World using eval

The eval command can be useful to run simple, inline scripts.

So far, the programs we’ve written have been quite simple. We just logged values to the console in a few different ways. However, as we start getting closer to the real world, we know we’ll write more complex logic. With more complex logic comes more bugs and the need to debug our code.

After pressing the “Run” button below, type deno run my-first-deno-program.js in the terminal to execute the code.

console.log('Hello from deno');
Hello World program using Deno