How to build a Deno module?

Rogger Valverde
2 min readMay 27, 2020

--

Nowadays, the majority of modules are uploaded in npm, where libraries are made in JavaScript or Typescript. In my opinion is better to use Typescript for creating a package, so it could be used either in both cases (ts or js projects) since it should have their types already defined.

So, what about Deno?

Deno, use hard importing for ending files (i.e .js, .ts), so you must declare your imports using its complete reference name.

import { serve } from "https://deno.land/std@0.53.0/http/server.ts";

This is from one of Deno’s example from deno.land. This is how you can import a module that is registered in deno.land. In case a module is not yet registered, you can use its url for the raw code.

// @deno-types="https://raw.githubusercontent.com/roggervalf/iam-policies/master/dist/main.d.ts"import { IdentityBasedPolicy } from "https://raw.githubusercontent.com/roggervalf/iam-policies/master/dist/main.es.js";

Here you can notice that this lib is from github and is using Js and for defining its types, you should add the line at the top with the url reference for its type file (d.ts).

Even thought, there are already good libraries made in npm. As Deno use this kind of import, I have to maintain my lib bundle in single files to avoid having not hard imports, in this way you can still use this lib in Node as in Deno. Everybody win!

Using rollup

I recommend using rollup to bundle your library and write your code using Typescript. So for this approach, I would like to share my configuration:

And for tsconfig.json:

{
"compilerOptions": {
"target": "es5",
"module": "ES2015",
"declaration": true,
"outDir": "./dist",
"strict": true,
"noUnusedParameters": true,
"allowSyntheticDefaultImports": true,
"noImplicitThis": false
},
"include": ["src"],
"exclude": [
"node_modules",
"src/**/*.test.ts",
"build",
"dist",
"example",
"rollup.config.js"
]
}

This package is using this configuration. I hope this could help you.

--

--