Deno — Workers, Libraries, and Modules
Tags: deno, webdev, programming, javascript
Published: 2021-01-22 02:30:14

**Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62**

**Subscribe to my email list now at http://jauyeung.net/subscribe/**


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.

### Workers

Deno supports the web worker API.

For example, we can write:

`index.ts`

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

`worker.js`

```
self.onmessage = async (e) => {
  const { filename } = e.data;
  const text = await Deno.readTextFile(filename);
  console.log(text);
  self.close();
};
```

`log.txt`

```
hello
```

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=https://deno.land/std@0.75.0 index.ts
```

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

```
deno cache --reload=https://deno.land/std@0.75.0/fs/copy.ts,https://deno.land/std@0.75.0/fmt/colors.ts 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 "https://deno.land/std/fs/copy.ts";
```

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

```
import { copy } from "https://deno.land/std@0.75.0/fs/copy.ts";
```

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:

`math.ts`

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

`index.ts`

```
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 {
  add,
  multiply,
} from "https://x.nest.land/ramda@0.27.0/source/index.js";

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

We import the `add` and `multiple` functions from `https://x.nest.land/ramda@0.27.0/source/index.js` .

Then we can use them the same way.

### Conclusion

We can use web workers directly with Deno.

Also, we can import modules from various sources.