Moving a Node.js project to Deno helps you use a modern runtime with built-in security and simpler module management.
Migrating a Node.js project to Deno
Introduction
Syntax
Deno
import { serve } from "https://deno.land/std@0.201.0/http/server.ts"; serve((_req) => new Response("Hello from Deno!"), { port: 8000 });
Deno uses URL imports instead of package.json and node_modules.
Permissions must be explicitly granted when running Deno scripts.
Examples
Deno
import { readFileStr } from "https://deno.land/std/fs/mod.ts"; const data = await readFileStr("./file.txt"); console.log(data);
Deno
const decoder = new TextDecoder("utf-8"); const data = await Deno.readFile("./file.txt"); console.log(decoder.decode(data));
Deno
const response = await fetch("https://api.example.com/data"); const json = await response.json(); console.log(json);
Sample Program
This simple Deno server listens on port 8080 and responds with a greeting message.
Deno
import { serve } from "https://deno.land/std@0.201.0/http/server.ts"; serve((_req) => new Response("Hello from Deno!"), { port: 8080 });
Important Notes
Node.js uses CommonJS modules, but Deno uses ES modules with URL imports.
Deno requires explicit permission flags like --allow-read or --allow-net when running scripts.
Some Node.js built-in modules are not available in Deno; you may need to find Deno-compatible alternatives.
Summary
Deno uses URL imports instead of npm packages.
You must grant permissions explicitly in Deno.
Code changes may be needed to replace Node.js APIs with Deno APIs.
Practice
1. Which of the following is a key difference when migrating a Node.js project to Deno?
easy
Solution
Step 1: Understand module import differences
Node.js uses npm packages imported by name, while Deno uses direct URL imports.Step 2: Check permission handling and module system
Deno requires explicit permission grants and uses ES modules, not CommonJS.Final Answer:
Deno uses URL imports instead of npm packages. -> Option BQuick 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
Solution
Step 1: Identify Deno import style
Deno imports modules using URLs, not package names or require().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.Final Answer:
import _ from 'https://cdn.skypack.dev/lodash'; -> Option AQuick 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
Solution
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.Step 2: Analyze output
Since readFileStr is not a function, the script will throw an error.Final Answer:
Error: readFileStr is not a function -> Option AQuick 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
Solution
Step 1: Understand Deno's permission model
Deno requires explicit permission flags to access files for security.Step 2: Apply correct permission flag
Using--allow-readgrants read access to files, fixing the error.Final Answer:
Run the script with deno run --allow-read flag. -> Option CQuick 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
Solution
Step 1: Identify synchronous file read in Deno
Deno provides readTextFileSync() for synchronous text file reading.Step 2: Compare options
const data = Deno.readTextFileSync('./info.txt'); console.log(data); correctly uses readTextFileSync and logs the data synchronously.Final Answer:
const data = Deno.readTextFileSync('./info.txt'); console.log(data); -> Option DQuick 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
10 questions ~5 mineasy hard
