Bird
Raised Fist0
Denoframework~20 mins

Deno shims for Node.js APIs - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Deno Node.js Shim Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
How does Deno's Node.js shim handle the 'fs' module?
In Deno, when using the Node.js 'fs' shim, what happens if you call fs.readFileSync on a file path? Assume the file exists and is readable.
Deno
import * as fs from "https://deno.land/std/node/fs.ts";
const content = fs.readFileSync("./example.txt", "utf8");
console.log(content);
AIt throws a runtime error because synchronous file reading is not supported in Deno shims.
BIt reads the file content synchronously and returns a string with the file data.
CIt returns a Promise that resolves with the file content asynchronously.
DIt returns undefined because the shim does not implement readFileSync.
Attempts:
2 left
💡 Hint
Think about how Deno's Node.js shims aim to mimic Node.js APIs synchronously where possible.
📝 Syntax
intermediate
1:30remaining
Correct import syntax for Deno Node.js 'path' shim
Which import statement correctly imports the Node.js 'path' module shim in Deno?
Aconst path = require("https://deno.land/std/node/path.ts");
Bimport path from "https://deno.land/std/node/path.ts";
Cimport * as path from "https://deno.land/std/node/path.ts";
Dimport { path } from "https://deno.land/std/node/path.ts";
Attempts:
2 left
💡 Hint
Deno uses ES module syntax and the Node.js shims export all functions as named exports.
🔧 Debug
advanced
2:00remaining
Why does this Deno code using Node.js 'crypto' shim fail?
Consider this Deno code snippet: import { randomBytes } from "https://deno.land/std/node/crypto.ts"; const buf = randomBytes(16); console.log(buf); Why might this code throw an error in Deno?
Deno
import { randomBytes } from "https://deno.land/std/node/crypto.ts";
const buf = randomBytes(16);
console.log(buf);
ABecause Deno requires explicit permission to access crypto functions, and the permission is missing.
BBecause the 'randomBytes' function is not implemented in the Deno Node.js crypto shim.
CBecause the import URL is incorrect and does not point to a valid module.
DBecause 'randomBytes' returns a Promise and the code does not await it.
Attempts:
2 left
💡 Hint
Deno has a security model that requires permissions for certain operations.
❓ state_output
advanced
1:30remaining
What is the output of this Deno code using Node.js 'events' shim?
Analyze the following code and determine what it prints to the console:
Deno
import { EventEmitter } from "https://deno.land/std/node/events.ts";

const emitter = new EventEmitter();
emitter.on("greet", (name) => {
  console.log(`Hello, ${name}!`);
});
emitter.emit("greet", "Alice");
emitter.emit("greet", "Bob");
AHello, Alice!\nHello, Bob!
BError: emit is not a function
CHello, Alice!\nundefined
DNo output because events are not supported in Deno shims
Attempts:
2 left
💡 Hint
The events shim mimics Node.js EventEmitter behavior.
🧠 Conceptual
expert
2:30remaining
Which Node.js API is NOT fully supported by Deno shims and why?
Among the following Node.js core modules, which one is known to have incomplete or partial support in Deno's Node.js shims, and what is the main reason?
AThe 'events' module, because event emitters are not compatible with Deno's event loop.
BThe 'path' module, because file path handling is fundamentally different in Deno.
CThe 'fs' module, because Deno does not allow any file system access.
DThe 'net' module, because Deno uses a different networking API and restricts raw socket access.
Attempts:
2 left
💡 Hint
Consider Deno's security model and API design differences from Node.js.

Practice

(1/5)
1. What is the main purpose of Deno shims for Node.js APIs?
easy
A. To convert Deno code into Node.js code automatically
B. To allow using Node.js modules and APIs in Deno environment
C. To replace Deno's runtime with Node.js runtime
D. To compile TypeScript code into JavaScript

Solution

  1. Step 1: Understand what shims do

    Deno shims provide compatibility layers that mimic Node.js APIs inside Deno.
  2. 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.
  3. Final Answer:

    To allow using Node.js modules and APIs in Deno environment -> Option B
  4. Quick Check:

    Deno shims = Node.js API compatibility [OK]
Hint: Shims let Deno run Node.js code easily [OK]
Common Mistakes:
  • Thinking shims convert code automatically
  • Confusing runtime replacement with API compatibility
  • Assuming shims compile TypeScript
2. Which is the correct way to import the Node.js 'path' module shim in Deno?
easy
A. import path from 'https://deno.land/std/node/path.ts';
B. import { path } from 'https://deno.land/std/node/path.ts';
C. import * as path from 'https://deno.land/std/node/path.ts';
D. import path from 'node:path';

Solution

  1. Step 1: Check Deno shim import style

    Deno shims for Node.js modules are imported as namespaces using * as syntax.
  2. Step 2: Match the correct import statement

    Only import * as path from 'https://deno.land/std/node/path.ts'; correctly imports the shim as a namespace.
  3. Final Answer:

    import * as path from 'https://deno.land/std/node/path.ts'; -> Option C
  4. Quick Check:

    Namespace import = import * as [OK]
Hint: Use 'import * as' for Node.js shims in Deno [OK]
Common Mistakes:
  • 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
3. What will be the output of this Deno code using Node.js 'path' shim?
import * as path from 'https://deno.land/std/node/path.ts';
console.log(path.basename('/foo/bar/baz.txt'));
medium
A. 'baz.txt'
B. '/foo/bar/baz.txt'
C. 'baz'
D. SyntaxError

Solution

  1. Step 1: Understand path.basename behavior

    The basename function returns the last part of a path, including extension.
  2. Step 2: Apply to given path string

    For '/foo/bar/baz.txt', the basename is 'baz.txt'.
  3. Final Answer:

    'baz.txt' -> Option A
  4. Quick Check:

    path.basename('/foo/bar/baz.txt') = 'baz.txt' [OK]
Hint: basename returns last filename with extension [OK]
Common Mistakes:
  • Expecting the full path as output
  • Confusing basename with dirname
  • Syntax errors from wrong import (not here)
4. Identify the error in this Deno code using Node.js 'fs' shim:
import { readFileSync } from 'https://deno.land/std/node/fs.ts';
const data = readFileSync('./file.txt', 'utf8');
console.log(data);
medium
A. readFileSync does not accept encoding as second argument in Deno shim
B. The import path is incorrect, should be 'https://deno.land/std/node/fs.ts'
C. readFileSync is asynchronous and cannot be used like this
D. No error, code works as expected

Solution

  1. Step 1: Verify the import

    Named import { readFileSync } from Deno's std/node/fs.ts is valid as functions are named exports.
  2. Step 2: Check readFileSync arguments

    The function accepts encoding as a string ('utf8') as second argument, matching Node.js API.
  3. Final Answer:

    No error, code works as expected -> Option D
  4. Quick Check:

    readFileSync(path, 'utf8') valid [OK]
Hint: Named imports and string encoding work in Deno fs shim [OK]
Common Mistakes:
  • Believing encoding must always be an options object
  • Confusing with Deno's native synchronous APIs
  • Overlooking that fs shim closely mimics Node.js
5. You want to use the Node.js 'events' module shim in Deno to create an event emitter. Which code snippet correctly creates an emitter and listens for a 'ping' event?
hard
A. 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');
B. import events from 'https://deno.land/std/node/events.ts'; const emitter = new events.EventEmitter(); emitter.on('ping', () => console.log('pong')); emitter.emit('ping');
C. import EventEmitter from 'https://deno.land/std/node/events.ts'; const emitter = new EventEmitter(); emitter.on('ping', () => console.log('pong')); emitter.emit('ping');
D. import { EventEmitter } from 'node:events'; const emitter = new EventEmitter(); emitter.on('ping', () => console.log('pong')); emitter.emit('ping');

Solution

  1. Step 1: Check correct import style for 'events' shim

    The 'events' module shim exports as a namespace, so import * as events is correct.
  2. Step 2: Verify usage of EventEmitter and event methods

    Creating emitter with new events.EventEmitter() and using addListener or on works; here addListener is used correctly.
  3. Step 3: Confirm emit triggers listener

    Calling emitter.emit('ping') triggers the listener printing 'pong'.
  4. 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 A
  5. Quick Check:

    Namespace import + EventEmitter + addListener = correct [OK]
Hint: Use namespace import and new events.EventEmitter() [OK]
Common Mistakes:
  • Using default import which is invalid
  • Importing from 'node:events' which is Node.js native
  • Confusing on() and addListener() usage with wrong import
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard