Deno Modules

Deno Modules are sets of Deno files or folders collected in a single folder in order to be reused (import) anytime.

A module can also be a single file.

Advantages of Modules

  • Modules lets use separate your codes according to their functions.
  • Modules make it easy to distribute your codes to others
  • Modules can be imported and reused making your codes more readable and organized.

Importing Module In Deno

Importing a module in Deno also means reusing the codes.

Unlike Node.js which uses the global require() method, Deno does not use require(), Deno imports modules the same way it is done in JavaScript ES, using the import keyword, but Deno also supports remote importing using the module URL. This means that Deno supports module imports from both file path and URL.

Deno Create and Import Module with File Path

A module is another file but with the export keyword. The export keyword tells the Deno compiler that is is a module. The export has the name implies, means provides the object that will be available for import

We will create a single file module rand.js.

Steps to Create It

  • Create a file rand.js
  • write code that generates a random number between 0 and 2000 
  • store the result in random_to_5 constant and export it

The random_to_5 constant can then be imported into another file using the import keyword.

File: rand.js

const max_number = 5;

// Export a module to make it reusable
export const random_to_5 = Math.ceil(Math.random() * max_number);
  Do It Yourself

Now let import this module into our module_example.js.

File: module_example.js

// Import the random number module
import {random_to_5} from './rand.js';

// Use the imported module
console.log(random_to_5);
  Do It Yourself

Import All Module Properties

You can also import all module properties by using the  * ... as  keyword.

The line below imports all properties from the rand.js (including the random_to_5)

import * as all_props from './rand.js';

// Use the random_to_5 constant
console.log(all_props.random_to_5);

We have successfully imported our local module!

Deno Import Module From URL

To import module from URL, just specify the URL in the import section.

The following example imports server.ts from the standard std/http namespace. Then we create a web server with it.

Deno Import server.ts module From URL

//Import the serve() function in the server.ts module from URL
import { serve } from "https://deno.land/std/http/server.ts";

// serve on port 5000
const s = serve({ port: 5000 });

console.log('Listening to port 5000 on http://localhost:5000');

// Wait for request and response with a text
for await (const req of s) {
  req.respond({ body: "Hello Lyty!\n" });
}
  Do It Yourself

Example Explained

  • We import the serve() function from the server.js remote module. This module enables us to serve our files.
  • We serve it on a port 5000.
  • Wait for request and response with a string "Hello Lyty!"

What You Should Know at the End of This Chapter

  • You should be able to create your own module and export it.
  • You should be able to import the object from the create module.
  • You should be able to import a module remotely (from URL).