DEV Community

Jake Lacey
Jake Lacey

Posted on

Building your first Deno module

First let's understand how modules are loaded in Deno.

import aModule from 'https://some.com/thing/mod.ts'

We can understand a couple of things from this line
1) The modules are loaded in by URLs, so we need to host our modules somewhere?
2) The modules are loaded in directly from their Typescript code, so we don't need any complication step.

So where do we host our modules?

There are a couple of options,

  • You can either host them yourself, like push them to S3 or something like that.
  • You can raise a PR for them to be added to the Deno standard library.
  • If you are using Github to host your code then you can use their raw.githubusercontent.com service to access the raw code.

I think I prefer the third option of using Github and I'll get onto the reason why later down.

So how do we build a module?

I'm not going to go into a lot of detail of testing and all that stuff all I want to do is demonstrate how to build a basic module.

Step 1: Create a mod.ts

// inside mod.ts
const hello = (name: string = 'world') => `Hello ${name}`;
export default hello

Step 2: Create a repo on Github

Step 3: Push the changes to the repo

How do we use the module?

Well I called my repo https://github.com/jakelacey2012/deno-hello-world, so I'd import it something like this.

import hello from 'https://raw.githubusercontent.com/jakelacey2012/deno-hello-world/master/mod.ts'

hello('you');

Thats it you're ready to use the package :woop: :woop:

So why do I prefer this option?

  • I don't see a package like this needing to be part of the standard library.
  • I can easily control the version of the package by swapping out master to a release or another branch.

Thanks for reading

If you're having any issues with this, I'd suggest reading up on Deno. That being said feel free to ask questions in this repo https://github.com/jakelacey2012/deno-hello-world

Top comments (1)

Collapse
 
terkwood profile image
Felix Terkhorn

Nice tip on the github URLs. I like being able to point to a specific version!

raw.githubusercontent.com/Terkwood...