Front-end Development
How-tos & Guides
5 min read

How to use Node.js modules in Deno

Due to its power and first-class support for Typescript, Deno has gained a lot of popularity in recent years. A common stumbling block for developers new to Deno is how to use Node.js packages in Deno apps. In this tutorial, you'll learn how to leverage the enormous ecosystem of code in the npm registry and use Node.js modules in your Deno applications.

Ekekenta Clinton
Published July 12, 2022

What is Deno?

Deno is a V8-based runtime for Javascript and Typescript that was created by Ryan Dahl, the original creator of Node.js. Deno seeks to address perceived shortcomings in the design of Node.js, specifically around security, ease of use, and dependency management.

Some key differences between Node.js and Deno are:

How to use a Node module in Deno

Before we dive in, let’s take a quick look at the key differences that make it difficult for you to use Node.js modules in the Deno application.

Perhaps to mitigate these differences, Deno supports a number of Node compatibility layers, allowing us to use NPM packages that do not use non-polyfilled Node.js APIs.

Using the std/node library

The Deno std/node library provides a compatibility layer that provides “polyfills” for Node.js' built-in modules. It also makes it possible to import CommonJS modules into the Deno application.

The module package allows you to import and use Node.js and CommonJs modules using the createRequire function. This function will perform the necessary resolution logic and install the modules globally on your computer.

import { createRequire } from "https://deno.land/std/node/module.ts";

const require = createRequire(import.meta.url);

const path = require("path");

//import local modules

const routes = require("./routes.js");

You can load your CommonJS modules without including the file extension.

const routes = require("./routes");

The createRequire function uses Node.js resolution in node_modules to load the package/module. So you can import packages from the node_modules directory.

const package = require("packagename");

Before using the above method, you need to install the package you wish to import locally using the Node.js package manager (NPM).

Deno does not have access to the network or the filesystem unless you explicitly provide it. Since Deno’s default behavior is to block access to the filesystem, you’ll need to grant file read permissions in order to run and load Node modules. Specifically, you’ll need to add the --allow-read flag when running your Typescript program, as shown below.

deno run --allow-read index.ts

Importing Node modules using CDNs

Deno supports remote HTTP modules, and CDNs are designed to provide fast access to files by hosting them in locations throughout the world. These two concepts together provide an easy way to access code in the npm registry via Deno. Popular CDNs are ESM, Skypack, JSPM.

Importing modules using ESM

ESM is a CDN created specifically for Deno, but it can also be used to access npm packages as ES Module bundles because it addresses Deno’s issues. It uses esbuild to make it possible to use any npm package as an ES Module.

import { fs } from "https://esm.sh/file-system";

const rawdata = fs.readFileSync("file.ext");

console.log(rawdata);

ESM also provides the flexibility of importing a specific version of a Node.js module.

import { fs } from "https://esm.sh/file-system@2.2.2";

Importing modules using Skypack

Another CDN you can use to import your Node.js modules is Skypack. This CDN allows developers to import and use modules that are not locally installed, which makes it easy to create Deno applications that leverage code directly from the npm registry.

import fs from "https://cdn.skypack.dev/file-system";

const rawdata = fs.readFileSync("index.ts");

console.log(rawdata);

You can also import a specific version of a module with Skypack, as shown below.

import fs from "https://cdn.skypack.dev/file-system@2.2.2";

Importing modules using JSPM

You can import Node.js modules using the JSPM CDN in a similar way. JSPM supports import maps by providing npm and other registry packages as ES Modules. While it doesn’t currently support Deno, the fact that Deno can use import maps means you can use the JSPM generator to create an import-map for all of the packages you want to use and have them delivered through CDN using the JSPM generator.

import fs from "https://dev.jspm.io/file-system";

Conclusion

Through this tutorial, you’ve learned the various ways to use Node.js modules in your Deno applications. Now that we’ve walked through the essentials, how would you use Deno in your next project?

To learn more about using Node.js modules in Deno, and for more advanced implementations, check out the Deno documentation.

Get started with Reflect today

Create your first test in 2 minutes, no installation or setup required. Accelerate your testing efforts with fast and maintainable test suites without writing a line of code.

Copyright © 2022 Reflect Software Inc. All Rights Reserved.