Deno shims let you use Node.js features in Deno. They help your code work smoothly when switching between Deno and Node.js.
Deno shims for Node.js APIs
import { shimName } from "https://deno.land/std/node/shimName.ts";
Replace shimName with the Node.js API you want to use, like path or buffer.
Use these shims to access familiar Node.js modules in Deno.
path shim to join folder and file names like in Node.js.import * as path from "https://deno.land/std/node/path.ts"; const fullPath = path.join("folder", "file.txt"); console.log(fullPath);
Buffer shim to create a buffer from a string and print it.import { Buffer } from "https://deno.land/std/node/buffer.ts"; const buf = Buffer.from("hello"); console.log(buf.toString());
This program uses the Deno shim for Node.js path to join folder and file names. It prints the combined path.
import * as path from "https://deno.land/std/node/path.ts"; function showFilePath(folder, file) { return path.join(folder, file); } console.log(showFilePath("docs", "readme.md"));
Deno shims are not full replacements but cover many common Node.js APIs.
Always check if the shim supports the features you need before using it.
Using shims helps keep your code cross-platform between Deno and Node.js.
Deno shims let you use Node.js APIs in Deno easily.
They help when migrating or sharing code between Deno and Node.js.
Import shims from https://deno.land/std/node/ and use them like Node.js modules.
Practice
Solution
Step 1: Understand what shims do
Deno shims provide compatibility layers that mimic Node.js APIs inside Deno.Step 2: Identify the purpose of these shims
The main goal is to let developers use Node.js modules and APIs in Deno without rewriting code.Final Answer:
To allow using Node.js modules and APIs in Deno environment -> Option BQuick Check:
Deno shims = Node.js API compatibility [OK]
- Thinking shims convert code automatically
- Confusing runtime replacement with API compatibility
- Assuming shims compile TypeScript
Solution
Step 1: Check Deno shim import style
Deno shims for Node.js modules are imported as namespaces using* assyntax.Step 2: Match the correct import statement
Onlyimport * as path from 'https://deno.land/std/node/path.ts';correctly imports the shim as a namespace.Final Answer:
import * as path from 'https://deno.land/std/node/path.ts'; -> Option CQuick Check:
Namespace import = import * as [OK]
- Using default import syntax which is invalid here
- Trying named import when module exports namespace
- Using 'node:path' which is Node.js native, not Deno shim
import * as path from 'https://deno.land/std/node/path.ts';
console.log(path.basename('/foo/bar/baz.txt'));Solution
Step 1: Understand path.basename behavior
Thebasenamefunction returns the last part of a path, including extension.Step 2: Apply to given path string
For '/foo/bar/baz.txt', the basename is 'baz.txt'.Final Answer:
'baz.txt' -> Option AQuick Check:
path.basename('/foo/bar/baz.txt') = 'baz.txt' [OK]
- Expecting the full path as output
- Confusing basename with dirname
- Syntax errors from wrong import (not here)
import { readFileSync } from 'https://deno.land/std/node/fs.ts';
const data = readFileSync('./file.txt', 'utf8');
console.log(data);Solution
Step 1: Verify the import
Named import{ readFileSync }from Deno's std/node/fs.ts is valid as functions are named exports.Step 2: Check readFileSync arguments
The function accepts encoding as a string ('utf8') as second argument, matching Node.js API.Final Answer:
No error, code works as expected -> Option DQuick Check:
readFileSync(path, 'utf8') valid [OK]
- Believing encoding must always be an options object
- Confusing with Deno's native synchronous APIs
- Overlooking that fs shim closely mimics Node.js
Solution
Step 1: Check correct import style for 'events' shim
The 'events' module shim exports as a namespace, soimport * as eventsis correct.Step 2: Verify usage of EventEmitter and event methods
Creating emitter withnew events.EventEmitter()and usingaddListeneroronworks; hereaddListeneris used correctly.Step 3: Confirm emit triggers listener
Callingemitter.emit('ping')triggers the listener printing 'pong'.Final Answer:
import * as events from 'https://deno.land/std/node/events.ts'; const emitter = new events.EventEmitter(); emitter.addListener('ping', () => console.log('pong')); emitter.emit('ping'); -> Option AQuick Check:
Namespace import + EventEmitter + addListener = correct [OK]
- Using default import which is invalid
- Importing from 'node:events' which is Node.js native
- Confusing on() and addListener() usage with wrong import
Ready to prove it?
Pass the test and this pattern moves unknown
