My DenoJS Learning Journals: Installation and First Try

dendihandian

Dendi Handian

Posted on June 16, 2020

My DenoJS Learning Journals: Installation and First Try

DenoJS is the 'new' javascript that initialy released in 2018, made by the creator of NodeJS, Ryan Dahl. DenoJS was made to 'start over' the NodeJS without the things that Ryan Dahl regretting in creating the NodeJS.

So this 'language' is getting NodeJS developers' attention and made them curious about what great about the platform itself and the differences to the NodeJS, including me.

Then I decided to try it myself and make a series about this learning.


The Installation of Deno

Because I'm using Windows on my machine, so I think the best way to install it is by using Chocolatey. I have Chocolatey installed on my machine. By following the official installation guide, I just need to open the PowerShell console with Administrator mode and type this command:

choco install deno
Enter fullscreen mode Exit fullscreen mode

And then after it finish installed, I saw this when I type deno and ENTER on the console:

PS C:\Users\dendi> deno
Deno 1.1.0
exit using ctrl+d or close()
>
Enter fullscreen mode Exit fullscreen mode

So I guess it was installed properly and perfectly.


The Blind Tries

So what happens if Deno run an index.js file with simple script like this?

const var1 = 1;
const var2 = 2;

console.log(`${var1} + ${var2} equals to ${var1 + var2}`);
Enter fullscreen mode Exit fullscreen mode

And it's still working!

PS C:\Users\dendi\playground> deno run .\index.js
1 + 2 equals to 3
Enter fullscreen mode Exit fullscreen mode

Because it's stated that Deno is fully-shipped with Typescript, now we try it with the .ts extension. So we change the index.js filename to index.ts and we run it again.

PS C:\Users\dendi\playground> deno run .\index.ts
Compile file:///C:/Users/dendi/playground/index.ts
1 + 2 equals to 3
Enter fullscreen mode Exit fullscreen mode

Now we see that it has compiling process before the output. Then modify the index.ts script and add an actual Typescript syntax like this:

const var1: number = 1;
const var2: number = 2;

console.log(`${var1} + ${var2} equals to ${var1 + var2}`);
Enter fullscreen mode Exit fullscreen mode

If you try to run the above script with NodeJS, then of course you will get errors because normally in NodeJS we need typescript and ts-node to compile it first.

And when we run the modified script with Deno, it's get the same output as before.


The official's getting started

Let's try with the 'getting started' script to replace the previous index.ts here:

import { serve } from "https://deno.land/std@0.57.0/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
  req.respond({ body: "Hello World\n" });
}
Enter fullscreen mode Exit fullscreen mode

When I ran it I got this:

PS C:\Users\dendi\playground> deno run .\index.ts
Download https://deno.land/std@0.57.0/http/server.ts
Download https://deno.land/std@0.57.0/encoding/utf8.ts
Download https://deno.land/std@0.57.0/io/bufio.ts
Download https://deno.land/std@0.57.0/_util/assert.ts
Download https://deno.land/std@0.57.0/async/mod.ts
Download https://deno.land/std@0.57.0/http/_io.ts
Download https://deno.land/std@0.57.0/io/util.ts
Download https://deno.land/std@0.57.0/textproto/mod.ts
Download https://deno.land/std@0.57.0/http/http_status.ts
Download https://deno.land/std@0.57.0/async/deferred.ts
Download https://deno.land/std@0.57.0/async/delay.ts
Download https://deno.land/std@0.57.0/async/mux_async_iterator.ts
Download https://deno.land/std@0.57.0/bytes/mod.ts
Download https://deno.land/std@0.57.0/path/mod.ts
Download https://deno.land/std@0.57.0/path/_constants.ts
Download https://deno.land/std@0.57.0/path/win32.ts
Download https://deno.land/std@0.57.0/path/posix.ts
Download https://deno.land/std@0.57.0/path/common.ts
Download https://deno.land/std@0.57.0/path/separator.ts
Download https://deno.land/std@0.57.0/path/_interface.ts
Download https://deno.land/std@0.57.0/path/glob.ts
Download https://deno.land/std@0.57.0/path/_util.ts
Download https://deno.land/std@0.57.0/path/_globrex.ts
Compile file:///C:/Users/dendi/playground/index.ts
error: Uncaught PermissionDenied: network access to "0.0.0.0:8000", run again with the --allow-net flag
    at unwrapResponse ($deno$/ops/dispatch_json.ts:43:11)
    at Object.sendSync ($deno$/ops/dispatch_json.ts:72:10)
    at Object.listen ($deno$/ops/net.ts:51:10)
    at listen ($deno$/net.ts:154:22)
    at serve (https://deno.land/std@0.57.0/http/server.ts:260:20)
    at file:///C:/Users/dendi/playground/index.ts:2:11
Enter fullscreen mode Exit fullscreen mode

I ran it again with deno run .\index.ts --allow-net and it still gives the same errors. And of course, I googled it. Then the right command to do it is:

deno run --allow-net .\index.ts
Enter fullscreen mode Exit fullscreen mode

Then you will have the your Hello world on http://localhost:8000/.

I conclude that the script uses http module to run the server. The module was downloaded at first execution, and I though it will always need the online network to run everytime but actually no. So I guess the module was stored somewhere on my machine and I will find the whereabouts later.

Okay, that's enough for the installation and first try of DenoJS.


💖 💪 🙅 🚩
dendihandian
Dendi Handian

Posted on June 16, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related