DEV Community

Cover image for First thoughts about Deno πŸ¦•
David Morcillo for codegram

Posted on

First thoughts about Deno πŸ¦•

Originally published at Codegram's blog

You may have heard about Deno at some point, or maybe not, but the internet is on fire right now and for a good reason! A new JavaScript runtime is out on the street and it has a cute dinosaur as a logo (some people think is a sad sock but no...). We even tweeted a week before its first release but to be honest we heard about Deno two years ago after watching this awesome talk by Ryan Dahl.

In this article we are going to explore the surface of Deno and share our first thoughts about it. Let's roll!

egg

Hello Deno!

Deno is a JavaScript runtime like Node. To use it, first you need to install the deno CLI using the instructions for your operating system in https://deno.land/ (kudos for such a great domain name!). Deno is not a fork of Node and it has been created from scratch using the Rust πŸ¦€ programming language. I wrote an article about Rust for JS programmers so check it out if you want to know more about the language!

After installing the CLI you can run the classic "Hello, World!" program like this:

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

Are we running TypeScript out of the box? Yes! TS is a first citizen in Deno so you don't need any additional tool to run TS code. You can also run JS code, but I am such a TypeScript fan that I mentioned it first 😜.

Be aware that Node programs are not compatible with Deno. They are working on a compatibility layer right now so it will be possible in the future to use your favorite NPM packages.

Another thing to notice is Deno is sandboxed by default. That means you will not have access to the network or your file system by default. If your program needs access you need to allow it explicitly (i.e. use --allow-net for network privileges).

No more package.json nor node_modules

Who hasn't heard about this recurrent joke around the internet?

node_modules_heavy

Well... even if it's a bit exaggerated, part of the message is true πŸ˜…. As your application grows, your dependency list grows with it and you end having a huge dependency tree in your system. The problem is each application has its copy of some package@version that you are using.

One of the main goals of Deno is to mirror how the browser works. When you want to import something in the browser you just add a <script> tag and use a URL. Deno works exactly like that:

import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
assertEquals("hello", "hello");
assertEquals("world", "world");
console.log("Asserted! πŸŽ‰");

Ok, that looks a bit odd! Since there is no package.json we don't have a way to specify external dependencies, so we are just importing them when they are needed! The first time you run this program Deno will automatically download the file and will cache it. You need to specify the --reload flag if you want the package to be downloaded again.

You will probably have tons of questions regarding this topic (I had them too!) so I invite you to visit this FAQ page in the Deno manual if you want more info about this topic.

Runtime compatible with Browser APIs

Another main goal of Deno is to be Browser compatible so there are some features that are accessible in the global scope like fetch or addEventListener. You can even use the window global object (although I recommend using the standard [globalThis](https://github.com/tc39/proposal-global) for now). You can check the documentation to learn more about these global functions.

The runtime also includes the Deno global for APIs that are not web standard. You can use it for some low-level operations like reading a file, opening a TCP socket, etc.

Rich standard library

Deno maintainers also created a collection of standard modules ready to use. The library is also hosted in the deno.land domain so you can import any module in your application using the url like this:

import { v4 } from "https://deno.land/std/uuid/mod.ts";

console.log(v4.generate());

The sandard library includes many useful modules but there are many third-party ones also available and the list is growing every day! The good news is you can import any module hosted in any public URL so importing modules from GitHub is also possible! Deno has a URL rewriting service that you can use to make your modules available. Check this page to learn more about it.

Built-in tooling

The deno CLI also includes some built-in tooling to perform some common tasks like running tests, formatting code or even creating a bundle! Does it mean you can replace jest, prettier and webpack? Probably not! Deno is pretty new and it doesn't have a huge ecosystem right now but having all this tooling with just installing one binary is pretty impressive.

Conclusions

Come aboard to the hype train! I am really impressed with Deno, after two years of development and with a long road to travel the new JavaScript runtime looks great and I can't wait to start experimenting with it. Node was released about 11 years ago and it was a remarkable milestone in the JavaScript world. It made the language much better and I think we have reached a new milestone: it's Deno πŸ¦• time!

Cover photo by Blanca Paloma SΓ‘nchez

Top comments (0)