📖 Scenario: You are building a simple Deno script that uses Node.js built-in modules to read a file and display its content. This helps you understand how Deno supports Node.js modules.
🎯 Goal: Create a Deno script that imports the Node.js fs module, reads a file named example.txt, and logs its content to the console.
📋 What You'll Learn
Import the Node.js fs module using Deno's compatibility layer
Create a variable filePath with the value 'example.txt'
Use fs.readFileSync to read the file content into a variable content
Log the file content as a string using console.log
💡 Why This Matters
🌍 Real World
Many developers use Deno to run JavaScript and TypeScript outside the browser. Supporting Node.js built-in modules helps reuse existing code and libraries easily.
💼 Career
Understanding how to work with Node.js modules in Deno is useful for backend developers who want to migrate or write cross-platform scripts and applications.
Progress0 / 4 steps
1
Import the Node.js fs module
Write an import statement to import fs from 'node:fs' using Deno's compatibility support.
Deno
Hint
Use import * as fs from 'node:fs'; to import the entire fs module.
2
Create the file path variable
Create a variable called filePath and set it to the string 'example.txt'.
Deno
Hint
Use const filePath = 'example.txt'; to create the variable.
3
Read the file content
Use fs.readFileSync with filePath to read the file content into a variable called content.
Deno
Hint
Use const content = fs.readFileSync(filePath); to read the file.
4
Log the file content as a string
Use console.log to print the content variable converted to a string using toString().
Deno
Hint
Use console.log(content.toString()); to display the file content.
Practice
(1/5)
1. What is the correct way to import the Node.js built-in path module in Deno?