0
0
Node.jsframework~20 mins

Writing files in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Writing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Node.js file write operation?
Consider this Node.js code snippet that writes to a file asynchronously. What will be logged to the console?
Node.js
import { writeFile } from 'node:fs/promises';

async function writeData() {
  try {
    await writeFile('test.txt', 'Hello World');
    console.log('Write successful');
  } catch (err) {
    console.log('Write failed');
  }
}

writeData();
ASyntaxError
BWrite failed
CNo output
DWrite successful
Attempts:
2 left
💡 Hint
The writeFile function from 'fs/promises' returns a promise that resolves on success.
component_behavior
intermediate
2:00remaining
What happens if you try to write to a file without permissions?
Given this Node.js code snippet, what will be the console output if the process lacks write permission to 'readonly.txt'?
Node.js
import { writeFile } from 'node:fs/promises';

async function writeData() {
  try {
    await writeFile('readonly.txt', 'Data');
    console.log('Write successful');
  } catch (err) {
    console.log(err.code);
  }
}

writeData();
AEACCES
BSyntaxError
CENOENT
DWrite successful
Attempts:
2 left
💡 Hint
EACCES is the error code for permission denied in Node.js.
📝 Syntax
advanced
2:00remaining
Which option correctly appends text to a file asynchronously?
Select the code snippet that appends 'More data' to 'log.txt' using Node.js fs/promises module.
A
import { writeFile } from 'node:fs/promises';
await writeFile('log.txt', 'More data', { flag: 'a' });
B
import { appendFile } from 'node:fs/promises';
await appendFile('log.txt', 'More data');
C
import { writeFile } from 'node:fs';
writeFile('log.txt', 'More data', { flag: 'a' });
D
import { appendFileSync } from 'node:fs/promises';
await appendFileSync('log.txt', 'More data');
Attempts:
2 left
💡 Hint
Appending can be done by writeFile with flag 'a' or appendFile function.
🔧 Debug
advanced
2:00remaining
Why does this code throw an error when writing a file?
Identify the cause of the error in this Node.js code snippet:
Node.js
import fs from 'fs/promises';

fs.writeFile('output.txt', 'Hello').then(() => {
  console.log('Done');
});

fs.writeFile('output.txt', 'World');
AwriteFile requires a callback function, missing here.
Bfs module import is incorrect, should be 'node:fs/promises'.
CSecond writeFile call is missing await or then, causing unhandled promise rejection.
DCannot call writeFile twice on the same file.
Attempts:
2 left
💡 Hint
Promises must be handled to avoid unhandled rejections.
🧠 Conceptual
expert
2:00remaining
What is the effect of using the 'wx' flag in writeFile options?
In Node.js fs/promises writeFile, what happens if you use the option { flag: 'wx' } when writing to a file?
AThe write operation is performed with exclusive lock preventing other writes.
BThe file is created and written only if it does not already exist; otherwise, an error is thrown.
CThe file is appended to if it exists; otherwise, it is created.
DThe file is overwritten regardless of existing content.
Attempts:
2 left
💡 Hint
The 'x' flag means exclusive creation in file system flags.