Deno Improves Node.js Support: Built-in Modules, package.json, Dynamic Import and Worker Compilation
MMS • Bruno Couriol
Article originally posted on InfoQ. Visit InfoQ
Recent releases of Deno (1.30, 1.31, and 1.32) make it easier for Node.js developers to transition to Deno projects. Deno 1.30 improves support for Node built-in modules and import maps. Deno 1.31 adds package.json support and includes the Node compatibility layer into the Deno runtime. Deno 1.32 adds deno compile
support for web workers and dynamic imports.
With Deno 1.30, developers can expose Node modules with node:
specifiers.
import { readFileSync } from "node:fs";
console.log(readFileSync("deno.json", { encoding: "utf8" }));
For developers using code with both Deno and Node.js, the node:
scheme will work in both runtimes. Deno’s documentation helpfully advises:
Take note that importing via a bare specifier (ex.
import { readFileSync } from "fs";
) is not supported. If you attempt to do so and the bare specifier matches a Node.js built-in module not found in an import map, Deno will provide a helpful error message asking if you meant to import with thenode:
prefix.
The deno.json
file can also now include import maps, eliminating the necessity to have two configuration files. To do so, developers specify imports
and scopes
keys in deno.json
. Import maps are now supported in all modern browsers and allow resolving module specifiers by matching them to a location (on disk or at a remote location).
With Deno 1.31, Deno will automatically detect a package.json
and use it to install and resolve dependencies. deno task
will also run scripts from the scripts
section of the package.json
.
For instance, the following code:
$ deno run -A npm:create-vite vite-project --template vue
$ cd vite-project
$ deno task dev
will run a default Vite project in dev
environment, as instructed by the template package.json
installed by the create-vite
scaffolding tool.
Deno 1.31 also includes the Node compatibility layer in the Deno runtime. The release note explains:
The Deno team has taken radical steps to improve the situation for users relying on npm packages – either via npm: specifiers or on the newly added package.json auto-discovery. The whole compatibility layer is now embedded in the Deno runtime itself, and V8 snapshots are used to drastically reduce the startup time. This tighter integration enables easier polyfilling of missing APIs and enhances the performance of already supported built-in Node.js modules.
Starting Deno 1.32, developers can use dynamic imports and Web Worker
API with binaries created with the deno compile
subcommand. The new feature makes it easier for developers to build executables for multi-threaded programs.
Deno is open-source software available under the MIT license. Contributions are encouraged via the Deno Project and should follow the Deno contribution guideline.