Node.js has many useful built-in modules for tasks like working with files or paths. Deno can use these modules too, so you can reuse code easily.
Node.js built-in module support in Deno
Introduction
Syntax
Deno
import { moduleName } from "node:moduleName";
Use the prefix "node:" before the module name to import Node.js built-in modules in Deno.
This works only for supported Node.js modules that Deno provides compatibility for.
Examples
Deno
import { readFileSync } from "node:fs"; const data = readFileSync("./file.txt", "utf-8"); console.log(data);
Deno
import { join } from "node:path"; const fullPath = join("/home", "user", "docs"); console.log(fullPath);
Deno
import { Buffer } from "node:buffer"; const buf = Buffer.from("hello"); console.log(buf.toString());
Sample Program
This Deno program uses Node.js built-in modules to join a file path and read the file content. It prints the path and the content.
Deno
import { join } from "node:path"; import { readFileSync } from "node:fs"; const filePath = join("./", "example.txt"); const content = readFileSync(filePath, "utf-8"); console.log(`File path: ${filePath}`); console.log(`Content: ${content}`);
Important Notes
Deno supports many but not all Node.js built-in modules.
Always use the "node:" prefix to import Node.js modules in Deno.
Check Deno documentation for the list of supported Node.js modules.
Summary
Deno can import Node.js built-in modules using the "node:" prefix.
This helps reuse Node.js code and libraries easily in Deno.
Use this feature when working with files, paths, buffers, and more.
Practice
1. What is the correct way to import the Node.js built-in
path module in Deno?easy
Solution
Step 1: Understand Deno's Node.js module import syntax
Deno uses the "node:" prefix to import Node.js built-in modules.Step 2: Apply correct import statement for
The correct import ispathimport path from "node:path";to access Node.jspathmodule in Deno.Final Answer:
import path from "node:path"; -> Option DQuick Check:
Node.js built-in modules use "node:" prefix in Deno [OK]
Hint: Use "node:" prefix to import Node.js built-ins in Deno [OK]
Common Mistakes:
- Omitting the "node:" prefix
- Using "deno:" prefix instead
- Trying to import from "path" without prefix
- Using incorrect module names
2. Which of the following is the correct syntax to import the Node.js
fs module in Deno?easy
Solution
Step 1: Recall import style for Node.js modules in Deno
Deno requires the "node:" prefix and supports namespace import for modules likefs.Step 2: Choose correct import syntax for
The correct syntax isfsimport * as fs from "node:fs";to import allfsexports as a namespace.Final Answer:
import * as fs from "node:fs"; -> Option AQuick Check:
Namespace import with "node:" prefix is correct [OK]
Hint: Use namespace import with "node:" prefix for modules like fs [OK]
Common Mistakes:
- Using default import for modules that export multiple members
- Omitting the "node:" prefix
- Trying to destructure import incorrectly
- Using "deno:" prefix
3. What will be the output of the following Deno code snippet?
import { join } from "node:path";
const fullPath = join("/home", "user", "docs");
console.log(fullPath);medium
Solution
Step 1: Understand
Thejoinfrom Node.jspathmodulejoinfunction combines path segments using the correct separator for the OS.Step 2: Analyze the given path segments
Segments "/home", "user", "docs" joined on Unix-like OS produce "/home/user/docs".Final Answer:
/home/user/docs -> Option BQuick Check:
Node.js path.join joins segments with OS separator [OK]
Hint: Node.js path.join joins paths with OS-specific separator [OK]
Common Mistakes:
- Assuming Windows separator on Unix
- Expecting error due to import
- Confusing join with concat
- Ignoring leading slash in first segment
4. Identify the error in this Deno code importing Node.js
buffer module:
import { Buffer } from "buffer";
const buf = Buffer.from("hello");
console.log(buf.toString());medium
Solution
Step 1: Check import path for Node.js built-in modules in Deno
Deno requires the "node:" prefix to import Node.js built-in modules likebuffer.Step 2: Identify the missing prefix causing module resolution error
Importing from "buffer" without "node:" prefix will fail in Deno.Final Answer:
Missing "node:" prefix in import path -> Option AQuick Check:
Always use "node:" prefix for Node.js built-ins in Deno [OK]
Hint: Always prefix Node.js modules with "node:" in Deno imports [OK]
Common Mistakes:
- Omitting the "node:" prefix
- Assuming default import works without prefix
- Confusing Deno modules with Node.js modules
- Misunderstanding Buffer API
5. You want to use the Node.js
events module in Deno to create an event emitter. Which code snippet correctly imports and uses it?hard
Solution
Step 1: Understand how Node.js
Theeventsmodule exports EventEmittereventsmodule exports EventEmitter as a named export, so namespace import is common.Step 2: Check correct import syntax with "node:" prefix and usage
Usingimport * as events from "node:events";thennew events.EventEmitter()is correct in Deno.Final Answer:
import * as events from "node:events"; const emitter = new events.EventEmitter(); -> Option CQuick Check:
Namespace import with "node:" prefix for EventEmitter [OK]
Hint: Use namespace import for EventEmitter from "node:events" [OK]
Common Mistakes:
- Using default import for EventEmitter
- Omitting "node:" prefix
- Importing from "events" without prefix
- Trying to destructure EventEmitter incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
