So you want to learn about Deno, the new JavaScript runtime? Well, you've come to the right place! This guide will teach you everything you need to know about Deno.
What is Deno?
Deno is a runtime for JavaScript, TypeScript, and WebAssembly that is based on the V8 JavaScript engine and the Rust programming language. It was created by Ryan Dahl, the original creator of Node.js, to remedy some the regrets he had when making Node.
Why Should I Use Deno?
Here are a few reasons why you might want to use Deno:
Secure by default. This means that it uses a sandbox to prevent code execution from outside the Deno process, and all network and file system operations are sandboxed by default.
Built-in support for TypeScript.
Ships with a set of standard libraries, which are written in TypeScript and are located in the Deno namespace. This means that you don't have to install third-party libraries to use Deno.
A plugin system that allows you to import third-party libraries as plugins. This means that you can import only the functionality that you need, and the plugin is sandboxed and can't access the Deno process.
A built-in test runner, which makes it easy to write and run tests for your Deno applications.
Open source and released under the MIT License.
Getting Started
Now that you know a little bit about Deno, let's get started!
First, you need to install Deno. The easiest way to do this is in with curl:
curl -fsSL https://deno.land/x/install/install.sh | sh
If you're on Mac, you can install Deno with a package manager like Homebrew.
brew install deno
Now that you have Deno installed, you can run Deno programs with the deno
command.
deno run https://deno.land/std/examples/cat.ts
This will fetch and run the cat.ts
program from the Deno Standard Library. The Deno Standard Library contains a collection of reusable Deno programs.
Now that you know how to run Deno programs, let's write our own! Create a file called hello.ts
and add the following code to it:
console.log("Hello, world!");
You can run this program with the deno
command:
deno run hello.ts
You should see the output Hello, world!
.
That's it! You've written your first Deno program!
Writing Deno Programs
Now that you know the basics of Deno, let's dive a bit deeper. In this section, we'll learn about some of the more advanced concepts in Deno.
Scripts and Modules
In Deno, there are two ways to structure your code: scripts and modules.
Scripts are simple programs that are run with the deno
command. Modules are program modules that can be imported by other programs.
Let's see an example of each.
First, let's create a script. Create a file called script.ts
and add the following code to it:
console.log("This is a script.");
You can run this script with the deno
command:
deno run script.ts
Next, let's create a module. Create a file called module.ts
and add the following code to it:
export function hello() {
console.log("Hello, world!");
}
This module exports a single function called hello
. We can import this function into another program and call it.
Create a file called main.ts
and add the following code to it:
import { hello } from "./module.ts";
hello();
This program imports the hello
function from the module.ts
module and calls it.
You can run this program with the deno
command:
deno run main.ts
You should see the output Hello, world!
.
Libraries
In Deno, libraries are collections of modules that are bundled together. Libraries can be published to Deno's Registry so that they can be easily reused by others.
Let's see an example of how to use a library. We're going to use the gravatar library to generate Gravatar URLs.
Create a file called main.ts
and add the following code to it:
import { gravatar } from "https://deno.land/x/gravatar/mod.ts";
console.log(gravatar("example@example.com"));
This program imports the gravatar
function from the gravatar
library and calls it with the email address example@example.com
.
You can run this program with the deno
command:
deno run main.ts
You should see the output https://www.gravatar.com/avatar/00000000000000000000000000000000?d=mp&s=80
.
Conclusion
You should now have a good understanding of Deno and be able to start writing your own programs. For more information, check out the Deno documentation.
Let me know in the comments if this was helpful or if you have any questions.
Be sure to follow me for more like this!