Deno First Look Deno First Look

Remote Scripts & Deno Object

Check out a free preview of the full Deno First Look course:
The "Remote Scripts & Deno Object" 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 discusses executing remote scripts using a URL in Deno and looks into operations found on the Deno object. The Deno object replacements for commonly used Node concepts including args, env, and _dirname are also covered in this segment.

Get Unlimited Access Now

Transcript from the "Remote Scripts & Deno Object" Lesson

[00:00:00]
>> Talk about executing remote scripts. So this is something that you'll see in deno a lot, and it's really weird. And it's one of the things that really put me off about it because it's sort of the way that they want you to run scripts. So this literally means that you can just call a script and execute it with a URL.

[00:00:17] And this is why if you go to the deno docs, they don't actually have you write hello world like we just did, they actually have you call it from a remote file. Which just is, that's such a bizarre thing to me to do and I had a really hard time with it.

[00:00:34] But let's do it so that we can see. So there is a script that we can run here. I'm just gonna paste this into the browser so we can look at it first. So if we go here, in our browser, we actually see the code. But if we were to curl this, I think we can just curl it like this.

[00:00:57] What we actually get back is the code not the web page. And what they're doing there is they're looking at the browser accepts header to know whether or not to give you a web page, or whether or not to give you code. But what this means is that you can execute this file directly from deno and so you can just do it.

[00:01:16] And let's clear this out a little bit. You can do deno run, and then like this and again it checks it and then it says welcome to deno and you get the cute dinosaur or denosore. But let's talk about where deno puts these remote dependencies cuz they're kind of in a different spot.

[00:01:39] So again, let's go to the denocache. And let me just double check to make sure that I'm, we're headed to the right place. And what we want to do now, is look at our deps folder because this is a dependency. So if we go to deps now, and we look inside of that, we see an https and inside of that, there's a couple of different ones because I've been working on some other stuff but this deno.land, right?

[00:02:02] It's pulling off the domain and using that as the name of the folder. So let's move into deno.land. And inside of that, where there's a lot of folder files. Let's take a look at them. So these are all of the different files that have been pulled down. And if we look at them, then eventually if we look at enough of them, we'll find the one, cuz I've been doing a lot with deno, we'll find that actual hello world file.

[00:02:34] But it downloads it here and then it creates that familiar hash, in all of this different metadata so that it can track dependencies. When we get to dependencies later on in this course, this is where deno is going to put them is inside of this folder called deps.

[00:02:50] So you don't have a node modules folder in deno, you have a deps folder that's inside a cache, which is something that you're not really aware of, right? So the dependencies are still there. I don't think it's fair to say, is it fair to say that you have no node modules folder in your project?

[00:03:06] It's just they're moved somewhere else on your system to a central place. Yeah, I think that's what we wanted to say about bad executing remote files. Okay. Let's talk about the Deno object because that's another something that's a little bit strange for deno, and I didn't understand this at first.

[00:03:30] In a browser, you have a top level object in a browser. It's called the window object. And the window object is just there. You don't have to do anything to get it. It's just exists. Deno has the same thing. But it's called the deno object. And much of the functionality that you might expect to find in particular node modules is in the deno object.

[00:03:56] So in VS code, let's come back here. Let's go to our app.ts file. And let's just look at that object. So if we look at it, and look at all the different methods that are available, there are things like MakeTempDir and MakeTempDirsync. If you've done node, this stuff looks familiar, or this this reading directories is here on this object.

[00:04:25] Reading files, writing files is down here. The standard out writing, to standard out is here. So a lot of IO operations are found directly on this deno object. In addition, you can get things like the args that were passed in. And so what's confusing for me about this is that deno also has a standard library and I wasn't, I'm never sure whether or not the thing that I need is on the deno object or in the standard library.

[00:04:59] And so my general guidance on this is that before you go look at deno standard library, which we haven't looked at yet, it's sort of something we have to install. It's a dependency, we have to use. Look on the deno object, because it's probably there, especially if it's something simple like reading and writing files, you can find it here.

[00:05:19] Working with environment variables, finding the current directory most of that's here. Let's talk about, for a minute about how deno replaces some concepts in node that you might already know. Deno has replaces some concepts. So for instance node has this process object. There is no process object in deno.

[00:05:41] Instead you would use the deno object and the replacements there in. And so, for instance in node, we can pass args on the command line and read them with the argv object. And so it's do, let's take a look at what that looks like in a node app.

[00:06:01] So in this app.js file, what I'm gonna do here is I'm just gonna log out console dot log process. Process.argsv, I think it's argv. So you can see what node actually does here, and then let's run this with the collapse this, collapse the sidebar. So let's do node app.js.

[00:06:33] And you can see that it's actually logging out all of the arguments here, including the path to node, the path to this file. And if we were to pass in other arguments like 1, 2, 3, then we'd get those as well. Deno does something similar, it has an args object here.

[00:06:51] So let's do this in the app.ts file, so we can see how this works. So let's just console.log, our deno.args. And then let's pass them args. So deno run app.ts 1 2 3, okay? And we get the same kind of thing only we're not getting the full path to node and the file as well.

[00:07:23] But this is the replacement for the args, if you're used to using that in node. Another thing you may be used to using is the env object in node, where we can pass in environment variables to node and then read them in. We use this a lot for determining whether or not we're in development, or production, or passing and things like connection strings.

[00:07:44] Deno doesn't have again the process objects. So how do we actually pass in environment variables? Well in deno, what we have instead is, let's call it take this out, we have Deno.env. And then you actually have to ask for the variable that you want. The way that this looks in node is quite different.

[00:08:09] So let's just say we're looking for a variable called DEVELOPMENT, which we're not passing in. Normally, in node what you'd do is you'd look for process.env.DEVELOPMENT. It comes as an actual part of the object. In Deno it does not. You actually have to get it like it's a string.

[00:08:29] So let's run this. Let me clear out the terminal. So let's do deno run app.ts and then we're not passing anything in. So a couple of different things have happened here. [LAUGH] So first of all we through an air and it tells us that the permission has been denied.

[00:08:45] So this is we're going to talk more about permissions. But this is really your first introduction to secure by default, which is that deno's not gonna let you do much of anything without you giving it permission to do so. So let's try this again. So let's do deno run app.ts.

[00:09:00] But before we specify, we have to say allow env, which allows it to read environment variables. And then we get undefined, if we were to pass in like this development equals true, then it should return true and it does. So that's just your replacement for the process.env in node and what that looks like when you do it in deno.

[00:09:25] Another one that you may be familiar with a node that you might use a lot is the dirname constant. by In node, the dirname constant tells you the full path of the current directory. In deno this constant doesn't exist it's not there. And so if you're gonna have to use this current working directory method, which gives you the same value.

[00:09:47] So let's take a look at that. Let's just come here and on the deno object, we'll do deno.currentworkingdirectory which is a function. And then let's try and run this again, deno run app.ts, and look at that, permission denied. Like I said, you're not allowed to do much of anything in deno, without giving it permission.

[00:10:11] So lets try that again, and it tells you it says run again with allow-read flag, so we'll just say allow-read, deno run, we'll see, oops. Allow-read app.ts and then it gives us the full path, right? And if you remember when we were looking at my gen files from deno, it put it in this folder structure here.

[00:10:34] Which is what we have on my machine. So that's your replacement for dirname in deno, which you'll probably use a lot is the current working directory. There's a lot more on the deno object than what we've just gone over there. But what I'm just trying to show you is that things that I think that you'll use the most often are the concepts in node that you already know that you might be looking to replace, like, how do I do this?

[00:10:54] I knew how to do it before, how do I do it now?