Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Built-in Modules Overview in Node.js
📖 Scenario: You are creating a simple Node.js script to explore some common built-in modules. This will help you understand how to use Node.js features without installing extra packages.
🎯 Goal: Build a Node.js script that imports the fs and path modules, reads the current directory files, and prints their names with their extensions.
📋 What You'll Learn
Import the built-in fs module
Import the built-in path module
Read the list of files in the current directory
Use path.extname to get each file's extension
Print each file name with its extension
💡 Why This Matters
🌍 Real World
Node.js built-in modules let you work with files, paths, and system info without extra installs. This is useful for scripts, servers, and tools.
💼 Career
Understanding built-in modules is essential for Node.js developers to build efficient and maintainable applications.
Progress0 / 4 steps
1
Import the fs module
Write a line to import the built-in Node.js module fs using import fs from 'fs'.
Node.js
Hint
Use the ES module syntax: import fs from 'fs'.
2
Import the path module
Add a line to import the built-in Node.js module path using import path from 'path'.
Node.js
Hint
Use the ES module syntax: import path from 'path'.
3
Read the current directory files
Write a line to read the list of files in the current directory using fs.readdirSync and store it in a variable called files.
Node.js
Hint
Use fs.readdirSync('.') to get files in the current folder.
4
Print each file name with its extension
Write a for...of loop to go through files. Inside the loop, use path.extname(file) to get the extension and print the file name and extension using console.log.
Node.js
Hint
Use a for...of loop and path.extname to get extensions.
Practice
(1/5)
1. Which statement best describes Node.js built-in modules?
easy
A. They require manual compilation before use.
B. They must be downloaded separately before use.
C. They are only available through third-party packages.
D. They are pre-installed tools for common tasks in Node.js.
Solution
Step 1: Understand what built-in modules are
Built-in modules come with Node.js by default and provide common functionalities without extra installation.
Step 2: Compare options with this fact
Only 'They are pre-installed tools for common tasks in Node.js.' correctly states they are pre-installed tools. Others mention downloading, third-party, or compiling, which are incorrect.
Final Answer:
They are pre-installed tools for common tasks in Node.js. -> Option D
Quick Check:
Built-in modules = pre-installed tools [OK]
Hint: Built-in means included by default, no download needed [OK]
Common Mistakes:
Thinking built-in modules need separate installation
Confusing built-in with third-party packages
Assuming manual compilation is required
2. Which is the recommended way to import the built-in fs module in Node.js using ES modules?
easy
A. import fs from 'fs';
B. import fs from 'node:fs';
C. import { fs } from 'fs';
D. const fs = require('fs');
Solution
Step 1: Identify ES module import syntax for built-in modules
Node.js recommends using the node: prefix with ES module import syntax for built-in modules.
Step 2: Check options for correct syntax
import fs from 'node:fs'; uses import fs from 'node:fs'; which is correct. import fs from 'fs'; misses the prefix, B uses CommonJS syntax, C incorrectly destructures.
Final Answer:
import fs from 'node:fs'; -> Option B
Quick Check:
Use import + node: prefix for built-in modules [OK]
Hint: Use 'import' with 'node:' prefix for built-in modules [OK]
Common Mistakes:
Using CommonJS require() in ES modules
Omitting 'node:' prefix for built-in modules
Incorrect destructuring import syntax
3. What will the following code output?
import os from 'node:os';
console.log(os.platform());
medium
A. The current operating system platform, like 'win32' or 'linux'.
B. An error because 'os' is not a valid module.
C. Undefined because platform() is not a function.
D. The Node.js version number.
Solution
Step 1: Understand the 'os' module and platform() method
The 'os' built-in module provides operating system info. The platform() method returns the OS platform string.
Step 2: Analyze the code output
The code imports 'os' correctly and calls platform(), so it prints the OS platform like 'win32', 'linux', or 'darwin'.
Final Answer:
The current operating system platform, like 'win32' or 'linux'. -> Option A
Quick Check:
os.platform() returns OS platform string [OK]
Hint: os.platform() returns your system's platform name [OK]
A. The number 123 should be a string to join paths correctly.
B. The import statement is incorrect; 'node:path' is invalid.
C. path.join cannot join more than two arguments.
D. console.log is missing parentheses.
Solution
Step 1: Check the import statement
Importing 'path' from 'node:path' is correct syntax for built-in modules.
Step 2: Analyze path.join arguments
path.join expects string arguments. Passing number 123 relies on implicit string coercion, which is not best practice and can lead to unexpected results.
Step 3: Identify the error
Best practice is to convert 123 to string before joining paths to avoid issues.
Final Answer:
The number 123 should be a string to join paths correctly. -> Option A
Quick Check:
path.join needs string args, numbers cause issues [OK]
Hint: All path.join args must be strings, not numbers [OK]
Common Mistakes:
Thinking 'node:path' import is invalid
Believing path.join limits arguments
Missing parentheses in console.log
5. You want to read a file asynchronously using the built-in fs module with promises. Which code snippet correctly imports and uses it?
hard
A. import fs from 'node:fs';
fs.readFile('file.txt', (err, data) => { console.log(data); });
B. import fs from 'fs';
const data = fs.readFileSync('file.txt');
console.log(data);
C. import { readFile } from 'node:fs/promises';
const data = await readFile('file.txt', 'utf8');
console.log(data);
D. const fs = require('fs/promises');
fs.readFile('file.txt').then(console.log);
Solution
Step 1: Identify correct import for promise-based fs
Node.js provides promise-based fs functions under 'node:fs/promises' module, imported with ES module syntax.
Step 2: Check usage of readFile with await
import { readFile } from 'node:fs/promises';
const data = await readFile('file.txt', 'utf8');
console.log(data); correctly imports readFile from 'node:fs/promises' and uses await to read file asynchronously.
Step 3: Verify other options
import fs from 'node:fs';
fs.readFile('file.txt', (err, data) => { console.log(data); }); uses callback style, not promises. import fs from 'fs';
const data = fs.readFileSync('file.txt');
console.log(data); uses synchronous readFileSync. const fs = require('fs/promises');
fs.readFile('file.txt').then(console.log); uses require() which is CommonJS, not ES modules.
Final Answer:
import { readFile } from 'node:fs/promises';
const data = await readFile('file.txt', 'utf8');
console.log(data); -> Option C
Quick Check:
Use 'node:fs/promises' with await for async file read [OK]
Hint: Use 'node:fs/promises' and await for async file reading [OK]