Deno: Learn by Example
Ahmad AwaisDeno 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 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.
- 📦 bundler
- 🐛 debugger
- 🤖 test runner
- 🧶 code formatter
- 📖 docs generator
- 🗃 dependency inspector
- 🧵 WebAssembly support
More interesting features include…#
- Secure by default.
No file, network, or environment access, unless explicitly enabled. - Single Executable.
Ships only a single executable file.
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 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:
- zsh
- bash
- fish
- elvish
- Powershell
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:
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();
}
Deno.args
are the arguments received by this fileDeno.open()
opens a fileDeno.copy(file, Deno.stdout)
copies the file from the source to the destination i.e. the standard output
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
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
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
…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 →
Developers Takeaway
Stay ahead in the web dev community with Ahmad's expert insights on open-source, developer relations, dev-tools, and side-hustles. Insider-email-only-content. Don't miss out - subscirbe for a dose of professional advice and a dash of humor. No spam, pinky-promise!
Bruce
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
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.