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
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 D
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
Step 1: Check correct import style for 'events' shim
The 'events' module shim exports as a namespace, so import * as events is correct.
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.
Step 3: Confirm emit triggers listener
Calling emitter.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 A