One of the core features of Deno is that it doesn’t rely on a centralized package server like Node.js did with npm. Anyone can publish a module to their own server and it’s very simply to use them, thanks to Deno resolving URLs for modules. Let’s see how to create, publish, and use our first Deno module.
What are Deno modules?
Deno modules are pieces of that you can download and use in your project, similar to Node.js packages. They can range from being a simple function to something complex like Oak, a middleware framework for Deno’s http module.
Deno provides a set of standard modules like the http module that are reviewed by the core Deno team. These are guaranteed to work along with a specific Deno version and live in the same denoland/deno repository where the Deno source code can be found.
The standard modules are hosted at deno.land/std and can be accessed via URLs like all other Deno-compatible ES modules.
But Deno also supports third-party modules like Oak that you can import from any location on the web, like GitHub, a personal webserver, or a CDN like pika.dev or jspm.io.
To make it easier to consume third party modules Deno provides some built in tools like
deno info and
deno doc. Additionally, Deno’s website also provides a web UI for viewing module documentation, available at doc.deno.land.
The website also provides a simple public hosting service for Deno-compatible ES modules that can be found at deno.land/x.
Writing your Deno module
Before publishing, we need to write our module. We’re going to create a simple to reverse a sequence of words. If we input “This is fun” it will return “fun is This”. Very simple. Create a new folder and the file for your module in it:
mkdir reverse-words cd reverse-words touch mod.ts
mod.ts in your IDE or code editor of choice, I’ll use VS Code. Paste this in it:
const reverseWords = (str: String) => str.split(" ").reduce( (revStr: Array<String>, word: String) => [word, ...revStr], , ).join(" "); export default reverseWords;
Note that this is TypeScript code. One of the great features of Deno is that it supports TypeScript natively. Since in the next step we’ll publish our Deno module to GitHub, you might want to add a simple
README.md with some info about it.
Publish your Deno module
One final step before publishing your module in case you’re interested in sending it to Deno’s repository, is to ensure it’s formatted according to Deno’s formatting standard. To do so, you can tell Deno to format your code by running:
deno fmt mod.ts
We’re going to publish the module to GitHub and will download it from there in our Deno project. Run:
git init git add . git commit -m "Initial commit"
Create a new repository on GitHub and leave it empty. GitHub will give you instructions to publish your repo, similar to
git remote add origin firstname.lastname@example.org:<your username or organization>/<name of your repo>.git git push -u origin master
This will publish your module to GitHub. Success! Now let’s use it.
Use your Deno module
Write a new file
index.ts and add this in it:
import reverseWords from 'https://raw.githubusercontent.com/startfunction/reverse-words/master/mod.ts'; console.log( reverseWords( 'This is fun' ) );
This will load the module you published in the previous step and will use it to reverse a string and log its result. We can run this with Deno:
deno run index.ts
it will display
fun is This
In the case of complex modules with many dependent modules, you might want to use Deno’s bundler that will compile everything in a single module for easier publishing. Have you published a Deno module? Let us know in the comments! And read our other articles about Deno like 10 great resources to learn and work with Deno.
1 thought on “Publish a Deno module in 3 quick steps”
[…] a Deno module is simple. We covered this in detail in a previous post about creating and publishing a Deno module on GitHub. You need to initialize a local git […]