Deno First Look

Using Node Dependencies

Burke Holland

Burke Holland

Deno First Look

Check out a free preview of the full Deno First Look course

The "Using Node Dependencies" Lesson is part of the full, Deno First Look course featured in this preview video. Here's what you'd learn in this lesson:

Burke walks through using a routeParser node module by importing a routeParser dependency from npm. This process can be used to import and use other types of Node modules in Deno.


Transcript from the "Using Node Dependencies" Lesson

>> Okay, what about route parameters though? Query strings are simple enough, but if we wanted to parse route parameters, such as like instead of what if we wanted to do name/Burke right? And we wanted to pull Burke off of the name, route. This is how restful routes are usually defined where you have products, and then one, right?

And it returns the product with an ID of one. We're just gonna return the name, Burke. So, how do we pull that off of there? Now, that is something that we cannot do, there's no API that exists to do that. So, this is a point where as a node developer, you start to think about your situation and look around and say, well, I could use some complicated regex to do this.

You could Google this and look it up on Stack Overflow. How do I do this? But a lot of times, instead of doing any of that, we just turned to NPM. And we just look for an NPM module that will help us with route params. So let's go to NPM and see, right?

We might come over here and just be like, I need something that parses routes, like a route parser. Is there a route parser? Look, there is a route parser. This one right here. In fact there's a lot of them. But this one says an isomorphic, bullet proof, ninja ready route parsing matching reversing library.

And if we look at it, this is what I usually do to see whether or not a package is legit, I'm like, 45,000 downloads in a week, looks good, NPM install. So obviously a lot of people are using this, it's being used. And it says that it's isomorphic.

So if you're not familiar with the term isomorphic it's a really, really bad way to say that it works in both the browser and in Node. I don't know why we come up with these confusing terms, but we do. And so if it works in the browser, then it will probably work in Deno, most likely, most likely.

So we can use this. But how do we use an NPM package that we can't install? Well, there's a couple of different ways that we can do this. There are some services out there that allow you to directly access modules on NPM without installing them. One of them is called unpackage.

I don't know if that's how you say that, if it's UNPKG or unpackage, I say unpackage. But basically you can point it at a file that's on NPM. So for instance let me find one, like this one here, this is react. And then it return to you the actual file back.

The problem here though, is that Deno needs specific ECMA script module syntax in order to use these modules, okay. And we already talked about the fact that node didn't have that module system, for a lot of time, it's been using the the common js Module spec, which is that module.exports if you're familiar with that.

And so, the problem is that we can't use our this library unless it follows the right Import Export syntax. And we could look to see whether or not that's true by going over to their GitHub repo. And then let's look at, I mean, let's just go to their lib folder here.

And this route file is what we're gonna need. And if we scroll down to the bottom. Okay, so, this right here is a problem. This is that common js module syntax, I believe it's called, which means it's not gonna work with Deno. But most node modules use this.

So does this mean that we can't use this with Deno? No, actually, believe it or not, someone built a service to fix this for us on the fly. So there's another service called, I believe,, here it is, jspm. So what jspm does is it is a module CDN allowing any package from NPM to be directly loaded in the browser, and other js environments as a fully optimized native JavaScript module.

In other words, it converts it so that we can use that it has that proper Import Export syntax that complies with the ECMA Script specification. So, how do we bring it in? We just have to use jspm to grab it. So what we can do here, is add it here, so I'm gonna bring it in.

So you can see here I'm pulling from jspm route parser, and then the version here. And you'll notice here that, I'm gonna do this, that Deno does not care that this is not a Deno module, that it's not a TypeScript module, that it didn't come from Deno land, it does not care.

It is more than happy to fetch this file and try to run it. So let's do that. Let's just fetch it. So it's gonna pull it down. Okay, so, now we've got it, so we've got it, but how exactly do we use it? The way that the docs for route parser are, so let's go back to their docs here.

[SOUND] route parser, is it any good? Yes, perfect, I love it. So it wants us to do this, route equals require route parser. Well, we've already got it in, and then var route equals new route, okay? So let's try that. So we say const route = new route.

New route, we call it routeParser, like this, and we don't get any IntelliSense. And this is because Deno needs TypeScript definitions for this file in order to know what to do here, it doesn't know anything about this. There's no TypeScript definitions. So we just need to bring in the TypeScript definitions for routeParser.

So where do we get those? Well, let's go back to And let's look for route parser is what was called, route parser. And then let's look for types. Route parser types. And certainly enough, look, right here. So there is a project called definitely typed, where people contribute TypeScript definitions for popular JavaScript libraries and route parser happens to be one of those.

So here we just need to install the type definitions. Well, how do we do that? Well, fortunately, we can use this unpackage service to bring in this types package into our file here. So I'm just gonna copy this down here cuz it's exactly the right syntax that we need, but basically what we need here, if we go back, if we go to the repo, what we need is we need to pull in.

Let's see, where's route parser? I think it's under types. Let's go to route parser. There's a lot of folders in here. I don't know if GitHub can even load them all. No, it can't. We won't even try. Anyway, there's a folder called route parser. And in there is an index.d.ts file that we need.

And we're gonna use that unpackage service to bring that in. So it's importing it drectly from NPM here. And again, we just fetch this. We're gonna fetch it. And if you remember it's sticking it in that cache, right? So if we were to go in the cache, we'll find these dependencies there.

And now, we just need to tell this routeParser, that it is of type routeParser. How do we do that? Well, there's a way that we can do that explicitly. Where we can just tell it Route is a routeParser as typeof RouteParser. So const Route = routeParser, which is this first one we've imported as typeof RouteParser, which are the TypeScript definitions.

Okay, and now that we've got that, we should be able to say const route = new Route. And look at that, now we get typescript definitions, we've got the IntelliSense, Deno is gonna be okay with us using this module. So let's, just to make sure I have the syntax right here, let's check with the guide, create a new route, that's right.

So let's define, let's go ahead and get rid of this noise here. Let's grab this. Let's pull it down. Let's pull it down, down, down, down here. And what we wanna do is define a new route and we wanna say /name, /:name like this. And really we could use /name/:name I think is what we did before, but we're just gonna look right on the route.

So if we pass in a name like this. Okay, and what this is doing is saying, there's a name parameter Route that's gonna come in, we wanna pull the name off. And so what we wanna do here is just say const match = route.match. And then our req.URL should do it.

And then here we don't need these query string params anymore because we're not looking at the query string. And we should be able to do, let's get rid of this too. What we should be able to do here is just say Is this working You know what we need to do, All the TypeScript people just cringed when I did that.

But we don't have a type for match. So we need to say that it's type any, sort of allow us to do this right here, reference this name. Okay, so, all of that. We're still running. Our watch is chugging along nicely. Let's head up here and see. If we put this in.

Hello burke. It's working. There we go, right?. So now we're successfully parsing routes. Now, I know that seems like a lot of hoops to jump through, but the whole point in that exercise was for you to see that it is technically possible for you to use node packages, NPM packages with Deno, you can do it.

It's just that you have to jump through a few hoops. Number one, because likely it doesn't have the right Import Export syntax. And also likely because you're gonna need the TypeScript definitions and then you're gonna have to tell Deno that something is of a particular type. So, a bit hacky, I think we could say, we can generally agree.

But technically, this does work.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now