Deno — Workers, Libraries, and Modules

Spread the love

Deno is a new server-side runtime environment for running JavaScript and TypeScript apps.

In this article, we’ll take a look at how to get started with developing apps for Deno.


Deno supports the web worker API.

For example, we can write:


const worker = new Worker(new URL("worker.js", import.meta.url).href, {
  type: "module",
  deno: true,
worker.postMessage({ filename: "./log.txt" });


self.onmessage = async (e) => {
  const { filename } =;
  const text = await Deno.readTextFile(filename);



Then when we run:

deno run  --unstable --allow-read index.ts

We create the worker with the Worker constructor.

We pass in the URL to the worker.

And the object specifies the type as 'module' and deno set to true so that we can run the worker.

worker.js has the worker.

self is the worker instance. The onmessage method accepts message passed in by calling postMessage .

And then we call Deno.readTextFile method to read a file in the worker.

We then should see hello logged in the console.

Reloading Modules

Deno caches modules by default without fetching or recompiling it.

If we need to reload them, we run:

deno cache --reload index.ts

to reload everything used by index.ts .

We can also reload a specific module used by index.ts by running:

deno cache --reload= index.ts

If we want to reload multiple modules used by index.ts , we run:

deno cache --reload=, index.ts

Lock Files

Deno stores and check the subresource integrity for modules using a small JSON file.

The -lock=lock.json flag enables and specifies lock file checking.

To update the lock file, we run:

--lock=lock.json --lock-write

We can use the  — -lock=lock.json option to check the data when we run our app.

Standard Library

We should run pinned versions of imports to avoid unintended changes.

For example, instead of writing:

import { copy } from "";

We should include the version number with the import by writing:

import { copy } from "";

We may use libraries that have unstable Deno APIs.

The copy module we have above is unstable, so to use it, we run:

deno run --allow-read --allow-write --unstable main.ts

to enable unstable APIs.

Import and Export Modules

We can use standard ES modules to with Deno apps.

For example, we can write:


export const add = (a: number, b: number) => a + b;
export const multiply = (a: number, b: number) => a * b;


import { add, multiply } from "./math.ts";

console.log(add(1, 2))
console.log(multiply(1, 2))

We have math.ts that exports the add and multiply functions.

Then we use them in index.ts .

Remote Import

We can also import modules directly from a remote URL.

For example, we can write:

import {
} from "";

console.log(add(1, 2))
console.log(multiply(1, 2))

We import the add and multiple functions from .

Then we can use them the same way.


We can use web workers directly with Deno.

Also, we can import modules from various sources.

Leave a Reply

Your email address will not be published. Required fields are marked *