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:
- Deno seeks to improve the security of applications by providing a more secure default configuration. By default, Deno has no access to the file system, network, or environment variables. Developers must explicitly choose to enable access.
- There is no equivalent to a ‘package.json’ in Deno. Instead, dependencies are imported by referencing package URLs directly in your Typescript code. Deno provides tools for inspecting the dependency cache (deno info) and pre-loading the cache (deno cache). The latter command is particularly useful in CI pipelines where you want to avoid downloading dependencies over the public internet, either for speed reasons or to prevent network failures from causing builds and deployments to fail.
- Working with TypeScript does not require any configuration in Deno, although you can provide one if you want to change any configuration parameters used by the TypeScript compiler (tsc).
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.
-
Node.js supports both ES and CommonJS, while Deno only supports ES Modules.
-
Deno performs module resolution at runtime, while Node.js depends on a
node_modules
andpackage.json
in the project directory to resolve modules. -
Deno supports remote import to fetch modules and cache them on the local computer, while Node.js depends on the modules installed using npm into the
node_modules
folder.
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.
|
|
You can load your CommonJS modules without including the file extension.
|
|
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.
|
|
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.
|
|
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.
|
|
ESM also provides the flexibility of importing a specific version of a Node.js module.
|
|
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.
|
|
You can also import a specific version of a module with Skypack, as shown below.
|
|
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.
|
|
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.