Bird
Raised Fist0
Denoframework~30 mins

Migrating a Node.js project to Deno - Mini Project: Build & Apply

Choose your learning style10 modes available
Migrating a Node.js Project to Deno
📖 Scenario: You have a simple Node.js project that reads a JSON file and logs its content. You want to migrate this project to Deno, which uses modern JavaScript and TypeScript features with a secure runtime.
🎯 Goal: Build a Deno script that reads a JSON file and logs its content, replacing the Node.js require and fs modules with Deno's native APIs.
📋 What You'll Learn
Create a JSON file named data.json with specific content.
Create a Deno script that reads data.json using Deno's file system API.
Parse the JSON content and store it in a variable.
Log the parsed JSON content to the console.
💡 Why This Matters
🌍 Real World
Migrating Node.js projects to Deno helps leverage Deno's secure runtime, modern APIs, and built-in TypeScript support for better developer experience.
💼 Career
Understanding how to migrate codebases to Deno is valuable for developers working on modern JavaScript runtimes and improving application security and performance.
Progress0 / 4 steps
1
Create the JSON data file
Create a file named data.json with this exact content: {"name": "Deno", "type": "runtime", "language": "TypeScript"}
Deno
Hint

Use a text editor to create data.json with the exact JSON content.

2
Set up the Deno script to read the file
In a new file named readData.ts, write a line to import the readTextFile function from Deno's standard library: import { readTextFile } from "https://deno.land/std/fs/mod.ts";
Deno
Hint

Use the exact import statement to get readTextFile from Deno's standard library.

3
Read and parse the JSON file
Add an async function named readJson that uses readTextFile to read data.json and parses it with JSON.parse into a variable called data.
Deno
Hint

Use await to read the file and JSON.parse to convert the text to an object.

4
Log the parsed JSON data
Inside the readJson function, add a line to log the data variable using console.log(data). Then call readJson() at the end of the file.
Deno
Hint

Use console.log(data) to show the JSON content and call readJson() to run the function.

Practice

(1/5)
1. Which of the following is a key difference when migrating a Node.js project to Deno?
easy
A. Deno uses CommonJS modules by default.
B. Deno uses URL imports instead of npm packages.
C. Deno requires no code changes from Node.js.
D. Deno automatically grants all permissions to the code.

Solution

  1. Step 1: Understand module import differences

    Node.js uses npm packages imported by name, while Deno uses direct URL imports.
  2. Step 2: Check permission handling and module system

    Deno requires explicit permission grants and uses ES modules, not CommonJS.
  3. Final Answer:

    Deno uses URL imports instead of npm packages. -> Option B
  4. Quick Check:

    Module import style = D [OK]
Hint: Remember: Deno imports modules via URLs, not npm names. [OK]
Common Mistakes:
  • Assuming Deno uses npm packages directly
  • Thinking Deno grants permissions automatically
  • Believing Node.js code runs unchanged in Deno
2. Which syntax correctly imports the lodash library in Deno?
easy
A. import _ from 'https://cdn.skypack.dev/lodash';
B. const _ = require('lodash');
C. import _ from 'lodash';
D. import _ from 'npm:lodash';

Solution

  1. Step 1: Identify Deno import style

    Deno imports modules using URLs, not package names or require().
  2. Step 2: Check each option

    import _ from 'https://cdn.skypack.dev/lodash'; uses a valid URL import from a CDN, which is correct for Deno.
  3. Final Answer:

    import _ from 'https://cdn.skypack.dev/lodash'; -> Option A
  4. Quick Check:

    URL import syntax = C [OK]
Hint: Use full URLs for imports in Deno, not package names. [OK]
Common Mistakes:
  • Using require() which is Node.js only
  • Importing by package name without URL
  • Using npm: prefix which Deno does not support
3. What will be the output when running this Deno script?
import { readFileStr } from 'https://deno.land/std/fs/mod.ts';
const data = await readFileStr('./file.txt');
console.log(data.trim());
Assuming file.txt contains " Hello Deno \n".
medium
A. Error: readFileStr is not a function
B. Hello Deno
C. undefined
D. oneD olleH

Solution

  1. Step 1: Understand readFileStr usage

    The function readFileStr was deprecated and removed in recent versions of Deno std library. Using it will cause an error.
  2. Step 2: Analyze output

    Since readFileStr is not a function, the script will throw an error.
  3. Final Answer:

    Error: readFileStr is not a function -> Option A
  4. Quick Check:

    Function availability = D [OK]
Hint: Use Deno.readTextFile or Deno.readTextFileSync instead of deprecated readFileStr. [OK]
Common Mistakes:
  • Expecting original spaces without trim()
  • Confusing readFileStr with non-existent function
  • Ignoring await causing undefined
4. You migrated a Node.js script to Deno but get this error: PermissionDenied: read access to './data.json' is not allowed. How do you fix it?
medium
A. Install the missing npm package for file access.
B. Change the file path to an absolute path.
C. Run the script with deno run --allow-read flag.
D. Use require() instead of import.

Solution

  1. Step 1: Understand Deno's permission model

    Deno requires explicit permission flags to access files for security.
  2. Step 2: Apply correct permission flag

    Using --allow-read grants read access to files, fixing the error.
  3. Final Answer:

    Run the script with deno run --allow-read flag. -> Option C
  4. Quick Check:

    Permission error fix = A [OK]
Hint: Add --allow-read flag to grant file read permission. [OK]
Common Mistakes:
  • Ignoring permission flags in Deno
  • Trying to fix path instead of permissions
  • Using Node.js require() which Deno disallows
5. You have this Node.js code snippet using the 'fs' module:
const fs = require('fs');
const data = fs.readFileSync('./info.txt', 'utf8');
console.log(data);
How should you rewrite it in Deno to read the file synchronously?
hard
A. const data = Deno.readFile('./info.txt'); console.log(data);
B. const data = await Deno.readTextFile('./info.txt'); console.log(data);
C. import fs from 'https://deno.land/std/fs/mod.ts'; const data = fs.readFileSync('./info.txt');
D. const data = Deno.readTextFileSync('./info.txt'); console.log(data);

Solution

  1. Step 1: Identify synchronous file read in Deno

    Deno provides readTextFileSync() for synchronous text file reading.
  2. Step 2: Compare options

    const data = Deno.readTextFileSync('./info.txt'); console.log(data); correctly uses readTextFileSync and logs the data synchronously.
  3. Final Answer:

    const data = Deno.readTextFileSync('./info.txt'); console.log(data); -> Option D
  4. Quick Check:

    Synchronous file read in Deno = A [OK]
Hint: Use Deno.readTextFileSync for sync file reading like Node's readFileSync. [OK]
Common Mistakes:
  • Using await with synchronous functions
  • Trying to import Node.js fs module in Deno
  • Using readFile which returns Uint8Array, not string
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard