JavaScript to TypeScript in Node.js

Deno, the new node.js

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

In this video I want to show you a new modern runtime that can execute typescript directly. This platform is been created by Ryan Dahl the creator of node.js.

Keyword

  • Deno the new node.js

Conflict of Interest

The author declares no conflict of interest.

About this video

Author(s)
Joris Hermans
First online
13 May 2022
DOI
https://doi.org/10.1007/978-1-4842-8203-8_27
Online ISBN
978-1-4842-8203-8
Publisher
Apress
Copyright information
© The Editor(s) (if applicable) and The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022

Video Transcript

Javascript to TypeScript and Node.js. Welcome to the third video, “Deno, the New Node.js.” So what is Deno? Something that we will learn. We will also look into how it differences from Node.js. And then, of course, we will work with Deno. I will execute a very basic HTTP server and show you how certain things are working within Deno.

So what is Deno? Well, Deno is an anagram for “node,” right? It’s been created by Ryan Dahl. And Ryan Dahl is also the creator of Node.js. So it’s created– that he created something new, better, and a little bit more modern. It’s a new JavaScript and TypeScript runtime. It uses rustlang under the hood, and it uses also the V8 engine like Node.js.

So what are the differences with Node.js? Well, Deno supports TypeScript out of the box. Deno supports also JavaScript out of the box, which is new and great, right? And we already saw a little bit of a solution from Node.js with ts-node. So that, yeah, with ts-node, we also get some kind of TypeScript out of-the-box solution, but it’s not ideal.

Of course, one of the big things, in my opinion, for Deno is no support for npm packages. Well, it’s also something that’s very hard, because if you are working with Node.js, and you want to go to Deno, well, yeah, it’s very hard to do so, because it doesn’t have the support of all the packages that you need. But also for that, I will give you some kind of solutions there.

It imports libraries with a URL. So you can do import, blah, blah, blah, from, and then you can paste the URL. And then it’s going to download your package from that URL. It uses a permission system for code to be executed. So if you want to reach your operating system, well, you need to give permission before you’re going to execute your code, which is also, for me, a very good thing to do because of security reasons.

In Node.js, you can just execute code which you don’t know what it’s under the hood, and definitely when you want to use third-party libraries. And for this one, that’s a good thing that we have some kind of permission– that you say, OK, something’s going to be read from the operating system. And we give permission for that. Also, the window object is supported in Deno.

So you can very easily install Deno, just the kernel, and then go to the install script of Deno itself. And then you just execute it, and it will be installed on your PC, where you will update certain parts of Deno. If you then do Deno version, you will get the version that’s been installed on your PC or your laptop.

You can very easily run something like this. Hello, right? And then you see already that is going to download that script. It says, “Warning– implicitly using latest version for welcome.ts.” And then he’s going to download the latest version of that. And then he’s going to say “welcome to.”

So one of the next steps could be creating an index.ts underneath our folder. And then we can execute it. Like I said, it has imports from a URL. So here, we need to import server.ts to get the TypeScript code from there.

And then, of course, we can use serve, and then we can start our localhost 8000. And then when we go to it, we always get a “Hello, world,” right? So if we do so, we need to do deno run index.ts. And then you will see, hey, we need to have information flag.

So what we could do now is do deno, run, dash, dash, allow, dash, net, because we need to give permission to execute and access our network. And then we can do index.ts. And now we gave the permission to use our network. So if you go down to localhost 8000, you will see that we get “Hello, world,” which is great, right? That will provide you with the knowledge that it’s working.

We saw differences with Node.js, and we worked with it. We created a file called index.ts, and we used server.ts. We also executed a welcome.ts so that you could see how it works and how he downloads the dependencies into our laptop.

So thank you for watching. I hope you enjoyed it. I hope you learned something from it. And I’ll see you in the next video, where we’re going to take it a little bit further by using work, a framework within Deno, to create APIs.