What if you could run your Node.js code in Deno without rewriting a single line?
Why Deno shims for Node.js APIs? - Purpose & Use Cases
Imagine you have a Node.js project with many modules using Node-specific APIs, and you want to run it in Deno without rewriting all the code.
Manually rewriting or adapting every Node.js API call to Deno equivalents is slow, error-prone, and can break your app's logic.
Deno shims for Node.js APIs provide ready-made compatibility layers so your Node.js code can run smoothly in Deno without major changes.
import fs from 'fs'; fs.readFileSync('file.txt');
import fs from 'node:fs'; fs.readFileSync('file.txt');
It enables seamless reuse of Node.js code in Deno, saving time and effort while expanding your app's reach.
A developer wants to run a popular Node.js library in Deno without rewriting it; shims make this possible instantly.
Manually adapting Node.js code for Deno is tedious and risky.
Deno shims provide a smooth compatibility layer for Node.js APIs.
This lets you run Node.js code in Deno easily and reliably.
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
