DEV Community

Cover image for Getting started with Deno
Conlin Durbin
Conlin Durbin

Posted on • Edited on

Dino Tutorial Getting started with Deno

Edit: Deno has hit 1.0 and with it, there has been some major discussion around whether the project needs an explicit CoC. The original maintainer seems to think it doesn't and I can't recommend that you use Deno until they do.
A Code of Conduct is a necessary part of any open source project.

In case you missed it, the new Javascript and Typescript runtime from Ryan Dahl, the creator of Node, has been released! It's got some really cool features and is ready for public use! Let's look at a few of the neat features and get started with a simple hello world!

What is Deno?

Deno is a new runtime for Typescript (and Javascript) written mostly in Rust. It has some great goals and some very interesting "Non-Goals", like not using npm and not having a package.json.

Getting it installed

Installing deno is as easy as running this command:

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

Then copy the export line and add it to your ~/bashrc or ~/bash_profile.

Open a new terminal and run deno. You should get a > prompt. Type exit and let's dig into some features!

Cool features in Deno

Typescript by default

Deno is integrated to run Typescript files by default. It basically makes types in Javascript a first-class citizen. No more compiling through Babel to use Typescript in server-side Javascript.

Importing from a URL

Deno lets you import from the web, just like you can in the browser. Just add a URL where you would usually name a module:

import { bgBlue, red, bold } from "https://deno.land/std/colors/mod.ts";
Enter fullscreen mode Exit fullscreen mode

A Standard Library

Furthermore, Deno has a standard library that is easy to import and use. There are modules that do a couple of different things, like HTTP handling, datetime work, and file system work. You can check it out here.

Uses ES modules

Finally, Deno only supports ES module syntax, which means no more require() statements, just good ole' import x from "y".

Hello World Example

Let's look at a quick Hello World that highlights a few of those features!

Copy this into a hello-world.ts file.

import { bgBlue, red, bold } from "https://deno.land/std/colors/mod.ts";

const sayHello = (name: string = "world") => {
  console.log(bgBlue(red(bold(`Hello ${name}!`))));
}


sayHello();

sayHello("Conlin");
Enter fullscreen mode Exit fullscreen mode

You can now run that with deno hello-world.ts and it should print out some stuff.

Change one of the sayHello calls to sayHello(15); and rerun it. You should see a type error since 15 isn't a string! That's pretty cool!

You'll also notice how to import from a URL - it's getting some console color stuff from the standard library!

Final thoughts

Deno isn't quite ready for the production use - there are a couple bugs, but development is moving forward quickly! This is definitely a cool new open source project and one to keep an eye on!

Top comments (29)

Collapse
 
stamper profile image
Alexander Toropov
Deno
A secure runtime for JavaScript and TypeScript 
Enter fullscreen mode Exit fullscreen mode

BUT!!!

Importing from a URL
Deno lets you import from the web, just like you can in the browser. Just add a URL where you would usually name a module:
import { bgBlue, red, bold } from "https://deno.land/std/colors/mod.ts";
Enter fullscreen mode Exit fullscreen mode

why? how it could be secure?!

Collapse
 
fvilante profile image
Flavio Vilante

Yes "Deno is safe", but that does not mean that the scripts that you will use it to execute are also.

Deno allows you to execute a nuclear bomb detonator if you wish.

But unlike Node, Deno will prevent you from lauching the bomb inadvertently.

Collapse
 
chromadream profile image
Jonathan Nicholas

It's secure because networking and other stuff are sandboxed inside a Rust sandbox, and network connection has to be explicitly granted.

Collapse
 
sqlrob profile image
Robert Myers

It's not, not unless you can pin a hash and limit hosts in configuration.

On the flip side, I don't think it's much worse than npm.

Collapse
 
johncarroll profile image
John Carroll • Edited

It has the possibility of being much secure than Node/NPM. Even if you allow network access, a script will not have access to the file system unless you also grant it file system access. The first time you run a script, you have the option of walking through everything in all the code which requires any additional permissions (file system, network, etc) and individually granting or denying access. Eventually, the goal is to allow for more fine grained access, e.g. scoping permissions to specific paths or specific URLs.

None of this is a silver bullet, but it's much better (more secure) than Node/NPM which just allows everything.

Collapse
 
nickytonline profile image
Nick Taylor • Edited

I first heard about Deno in his famous talk.

I'm a big fan of TypeScript as well, but from what I read previously, it won't play nice with anything node explicitly, correct? But I imagine there's nothing stopping you from importing JS as that is considered valid TS.

I'm definitely curious to see where Deno goes.

Collapse
 
wuz profile image
Conlin Durbin

Yeah, you should be good to just import JS - it just won't be typed. The big problem with node integration is the lack of modules. I don't think you can load an NPM package into Deno easily.

Collapse
 
bennypowers profile image
Benny Powers 🇮🇱🇨🇦

unless you use unpkg with ?module, in which case it's wicked-easy.

Thread Thread
 
nickytonline profile image
Nick Taylor

I think you could also reference a TS file from a github repo that's a TypeScript project and it'd work too maybe? I'll have to install Deno and try it out.

Thread Thread
 
johncarroll profile image
John Carroll • Edited

Deno only loads code via ES import statements (no require) using URLs or relative file paths that include the file extension. Additionally, deno does not support index.ts or index.js. So generally no drag and drop compatibility with node (though here's an example of a module that supports both deno and node: github.com/luvies/lazy). Still, probably pretty easy to create a migration script to make a project Deno compatible.

Collapse
 
devhammed profile image
Hammed Oyedele

Awesome, do you know? "De-No" - "No-De".

Collapse
 
somedood profile image
Basti Ortiz

Whoa, is that really why it's called Deno? If so, I genuinely applaud Dahl for that.

Collapse
 
devhammed profile image
Hammed Oyedele

Just saying, but it can be the reason.

Thread Thread
 
somedood profile image
Basti Ortiz

Oh. 😅 Still funny nonetheless.

Collapse
 
reactnativeguy profile image
Sudeepto Dutta

Hi @colin , I am React Native Developer, hoping to learn backend development with Node. Do you think Node is a good choice for beginners to start with ?

Will Deno be stable enough by 2020 to be considered production ready ? Do you think learning Node right now will not be useful as by the time I become proficient with Nodejs , Deno will be the norm ?

Apologies for the noob questions.

Thank You

Collapse
 
wuz profile image
Conlin Durbin

No worries! Honestly, I don't know. I think there will be a lot more stability by 2020, but I don't know what the infrastructure around Deno will be. There shouldn't be too much different between Node and Deno, at least for the purpose of backend development. They are both Javascript at the end of the day. It'll just be different resources you will be using to build out backends - like the express framework for Node vs the abc framework for Deno.

Hope that helps!

Collapse
 
reactnativeguy profile image
Sudeepto Dutta

Thanks for the input Conlin 🙂

Collapse
 
johncarroll profile image
John Carroll • Edited

Ryan (creator of Node and Deno) specifically addresses this question in this presentation: youtube.com/watch?v=z6JRlx5NC9E.

He says beginner developers should completely ignore Deno, that there is no guarantee Deno will still be alive in a year, and, even if Deno does exist in a year, Node will continue to exist for many, many more years to come.

Collapse
 
reactnativeguy profile image
Sudeepto Dutta

Thanks for the input John 🙂

Collapse
 
elanandkumar profile image
Anand Kumar • Edited

Looks like this article need to be updated. To run the file, we have to use deno run FILENAME.
And seems the color package is missing now at the url used in the example

Collapse
 
kamranayub profile image
Kamran Ayub

Liking it so far, very interested to see where it goes! Using deno fmt is nice too, which looks to be using Prettier.

Collapse
 
realabbas profile image
Ali Abbas

Hail Ryan, Deno is the future. 🖖

Collapse
 
codehexz profile image
CodeHexz

Great Article, But I was looking for a practical application of Deno and looking to get started with Deno and develop a REST API for my application. So I found a step-by-step guide here for developing a simple API using Deno, TypeScript & Oak Framework.

Collapse
 
antoninbeaufort profile image
antoninbeaufort

Hey, the import link has changed, the good now is https://deno.land/std/fmt/mod.ts

Collapse
 
aexol profile image
Artur Czemiel

I am kinda lazy and could've done it myself, but do you get autocomplete inside IDE for modules imported from URL?

Collapse
 
johncarroll profile image
John Carroll • Edited

Someone's already built a vs code extension for Deno flavor typescript: github.com/ameerthehacker/deno-vscode also github.com/justjavac/typescript-de....

Not sure if they currently support autocomplete, but, when IDEs do start supporting autocomplete for Deno modules, it'll probably be through an extension. It's also worth noting, that the recommended convention for a deno project is to only import from URL's once, in a single package.json like file called deps.ts and then re-export those modules locally from that file (example). That way you also ensure your entire project is using the same version of a module and you can change the version in a single place.

Given that Typescript itself is a node project, seems unlikely Deno will get any "official" support from the Typescript team anytime soon. That being said, one of the Deno project's goals is to provide a secure runtime for embedding javascript in larger applications. Given that Microsoft's CosmosDB database supports running javascript procedures natively (I assume through some kind of embedded VM), and given that Deno is built on Typescript (a Microsoft project), I could see Microsoft taking more/faster interest in Deno then they might ordinarily.

Collapse
 
wuz profile image
Conlin Durbin

Probably not at this point, but I am honestly not sure. If it doesn't exist, I'd guess it will be added soon!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.