Ahmad Awais

NAVIGATE


SHARE


Deno: Learn by Example

Ahmad AwaisAhmad Awais

Denobeginner.com

Deno 1.0 has just been released. It seems to be pretty awesome. Let’s learn deno and build a couple of example projects.

πŸ¦• I’ve released a free video course. Learn Deno for Beginners β†’

Deno#

Deno

Deno is a secure runtime for JavaScript and TypeScript. Imagine if you could write TypeScript without any config files, and bundle it all together into a single ES Module where both the TypeScript support and bundler are present in the core. That’s what it feels like when you get started with Deno.

It’s a modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust. Whereas Node.js is written in C++ and JavaScript.

Fun fact: Deno is an anagram of Node. If you sort() node it becomes deno.

"node".split("").sort().join(""); // Output: deno

Deno is also created by the creator of Node. Ryan Dahl. Yet deno is !node and node is !deno. OK, I’ll stop with the dad jokes now.

The core has many required features for writing modern JavaScript & TypeScript, and WebAssembly code.

More interesting features include…#

Install Deno#

With Shell:

curl -fsSL https://deno.land/x/install/install.sh | sh

With Homebrew:

brew install deno

Now check if deno was installed by running the deno --version command in your terminal.

Deno

Deno Completions#

You can generate a completions script for your shell using the deno completions command. The command outputs to stdout so you should redirect it to an appropriate file.

The supported shells are:

deno completions bash > /usr/local/etc/bash_completion.d/deno.bash
source /usr/local/etc/bash_completion.d/deno.bash

See deno install documentation for more installation options.

Deno Documentation#

Deno Code Examples#

Example #1: Deno Hello World#

Run the code:

deno run https://deno.land/std/examples/welcome.ts

The code for this file is a simple console log.

console.log("Hello Deno πŸ¦•");

The output should look like this:

Deno Hello World

Example #2: Deno cat Example#

Let’s implement a Unix cat program that outputs the contents of a file. Create a file called cat.ts with the following content in it.

// An implementation of the unix "cat" program.
for (let i = 0; i < Deno.args.length; i++) {
  let filename = Deno.args[i];
  let file = await Deno.open(filename);
  await Deno.copy(file, Deno.stdout);
  file.close();
}

Let’s run this file with the following command and read the welcome.ts that we wrote in the first example.

deno run cat.ts welcome.ts
Deno Permission Error

Uh-oh. Remember deno is secure by default. That means we need to allow the file read access to be able to run this program.

Let’s do that by reforming the command and this time with --allow-read flag right after the run keyword.

deno run --allow-read cat.ts welcome.ts
Deno Cat

Example #3: Deno Server Example#

Let’s create a deno server. The code for which looks pretty simple. Create a file called server.ts with the following code.

import { serve } from "https://deno.land/std/http/server.ts";

const server = serve({ port: 8000 });

console.log("Running at: http://localhost:8000/");

for await (const req of server) {
  const body = "Hello Deno!";
  req.respond({ body });
}

You might be thinking, what the heck is that await doing there. Well, deno implements a top-level await which is a new feature for ES Modules in the JavaScript V8 Engine. Basically it doesn’t need an enclosing async function to run await in the top-level scope.

Now let’s run the following command

deno run server.ts

Nops. That won’t work. We haven’t allowed it permission to access the net. Let’s allow access with --allow-net flag.

deno run --allow-net server.ts
Deno Server

…and that’s about it.

Interested in Learning Deno?#

If you would like to learn deno a bit deeper then sign up to my newsletter and you’ll hear from me sooner than later free Deno video course. For now, I’d appreciate it if you share this article on Twitter. And share your thoughts about deno.

Peace! ✌️

πŸ¦• I’ve released a free video course. Learn Deno for Beginners β†’

I help businesses understand developers πŸ“Ÿ Just launched Node.js CLI Automation Course πŸ‘¨β€πŸ« Edutainer at VSCode.pro 🎩 An award-winning GitHub Star open-source engineer & advocate 🦊 Google Developers Expert Web DevRel 🌳 Node.js foundation Community Committee Outreach Lead ✌️ Author of various open-source dev-tools and software libraries used by millions of developers worldwide ⓦ WordPress Core Developer πŸ“£ TEDx Speaker πŸ™Œ Leading developers and publishing technical content for over a decade πŸ’œ Loves his wife (Maedah) ❯ Learn more β†’

Say πŸ‘‹ on Twitter @MrAhmadAwais | Subscribe to the Developers Takeaway!

πŸ“¨

Developers Takeaway

Takeaway my professional opinion on open-source, developer relations, growing your business with family, and everything in between! Delivered to your inbox a couple of times every year. I'm even funny at times. I hate spam β€” pinky-promise!

πŸ‘¨β€πŸ’»πŸ‘©β€πŸ’» 118,947 Developers Already Subscribed
Comments 2
  • Bruce
    Posted on

    Bruce Bruce

    Reply Author

    Thanks for your examples. They’re a big help. I had a problem though.

    “deno run –allow-read cat.ts welcome.ts” generates: “error: Uncaught NotFound: The system cannot find the file specified. (os error 2)”

    But “deno run –allow-net server.ts” works fine.

    This happens only when using –allow-read –allow-write, and I see this in other examples I’ve tried. Any idea what’s wrong? I’m on Win10 and I installed Deno with chocolatey.


    • Ahmad Awais
      Posted on

      Ahmad Awais Ahmad Awais

      Reply Author

      First of all check out my free course on Deno called DenoBeginner.com β€” that might help. From the problem, it looks like your OS is not able to read the files. Maybe they don’t exist.