loading...
Cover image for Getting started with Deno

Dino Tutorial Getting started with Deno

wuz profile image Conlin Durbin Updated on ・2 min read

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";

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");

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!

Posted on by:

wuz profile

Conlin Durbin

@wuz

Software Engineer at @HackerRank 💻 Working on http://stdio.app ⚒️ writing at http://dev.to/wuz ⌨️ art+community+tech 👾🎨🏘 http://pronoun.is/he

Discussion

markdown guide
 
Deno
A secure runtime for JavaScript and TypeScript 

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";

why? how it could be secure?!

 

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.

 

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

 

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.

 

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.

 

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.

 

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.

 

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

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.

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.

 

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

 

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!

 
 

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.

 
 

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

 

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

 
 

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

 

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

 

Hail Ryan, Deno is the future. 🖖

 

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.

 

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

 

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

 

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.

 

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

 

Excellent, I'm very excited to start. Do you know where I can find more documentation?

 

Thanks to sharing for the process of Deno install, I have check one Deno tutorial from here hackr.io/tutorials/learn-deno