Deno is a modern runtime for JavaScript and TypeScript but it does not support all Node.js features directly. Knowing compatibility gaps helps you write code that works well in Deno.
Compatibility gaps and workarounds in Deno
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; serve((req) => new Response("Hello from Deno!"));
// Node.js style import (not supported directly in Deno) const fs = require('fs');
// Deno style import for file system import { readFileStr } from "https://deno.land/std/fs/mod.ts"; const content = await readFileStr("./file.txt");
// Using Deno permissions to read a file const data = await Deno.readTextFile("./file.txt");
This program shows how to create a simple HTTP server in Deno using its standard library. It handles the root path and returns a welcome message. This differs from Node.js where you might use the 'http' module.
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; // Simple HTTP server in Deno serve((req) => { const url = new URL(req.url); if (url.pathname === "/") { return new Response("Welcome to Deno server!", { status: 200 }); } else { return new Response("Not Found", { status: 404 }); } });
Deno does not support Node.js CommonJS modules by default; use ES modules with URL imports.
Some Node.js APIs like 'process' or 'Buffer' are not available or need polyfills in Deno.
Deno requires explicit permissions for file, network, and environment access for better security.
Deno uses URL-based ES module imports instead of Node.js package imports.
Many Node.js built-in modules have different APIs or need workarounds in Deno.
Understanding these gaps helps you write compatible and secure Deno applications.
Practice
Solution
Step 1: Understand Deno's import style
Deno uses ES modules imported via URLs or local paths, unlike Node.js which uses package names with require().Step 2: Compare with Node.js imports
Node.js uses CommonJS require() or ES modules with package names resolved from node_modules, which Deno does not support by default.Final Answer:
Deno uses URL-based ES module imports instead of Node.js package imports. -> Option AQuick Check:
Import style difference = A [OK]
- Thinking Deno supports require() like Node.js
- Assuming Deno imports only local files
- Believing Deno auto-converts Node packages
Solution
Step 1: Identify Deno's standard library import method
Deno imports standard modules via URLs from its official repository, e.g., https://deno.land/std/path/mod.ts.Step 2: Check each option
import path from "path"; uses Node.js style import without URL, invalid in Deno. const path = require("path"); uses require(), not supported in Deno. import { path } from "node:path"; uses Node.js built-in module specifier, not valid in Deno.Final Answer:
import * as path from "https://deno.land/std/path/mod.ts"; -> Option CQuick Check:
Standard library import = D [OK]
- Using require() instead of import
- Omitting the full URL for std modules
- Trying to import Node.js built-ins directly
import { readFileStr } from "https://deno.land/std/fs/mod.ts";
const data = await readFileStr("./example.txt");
console.log(data);
Assuming "example.txt" contains the text "Hello Deno!".Solution
Step 1: Understand the code behavior
The code imports readFileStr from Deno's standard fs module and reads the file content asynchronously.Step 2: Check for syntax and runtime correctness
The code uses top-level await, which Deno supports. The file exists with content "Hello Deno!" so readFileStr returns that string.Final Answer:
"Hello Deno!" -> Option BQuick Check:
File read content = A [OK]
- Assuming await needs async function wrapper
- Confusing readFileStr with returning undefined
- Thinking module URL is invalid
import crypto from "crypto"; but get an error. What is the best way to fix this?Solution
Step 1: Identify compatibility issue
Deno does not support Node.js built-in modules like "crypto" directly, so importing "crypto" fails.Step 2: Use Deno's alternative
Deno provides the Web Crypto API natively, which covers most cryptographic needs securely and without extra packages.Final Answer:
Use Deno's built-in Web Crypto API instead of Node.js crypto. -> Option DQuick Check:
Use Web Crypto API = B [OK]
- Trying to import Node.js crypto directly
- Assuming npm packages work without compatibility layer
- Using wrong import syntax for Node.js modules
Solution
Step 1: Understand the problem
Node.js libraries often rely on built-in modules like "fs" and "path" which Deno does not support natively.Step 2: Identify Deno's workaround
Deno provides a compatibility layer "deno_std/node" that shims many Node.js modules to allow such libraries to run with minimal changes.Final Answer:
Use a compatibility layer like "deno_std/node" to shim Node.js modules. -> Option AQuick Check:
Use compatibility shim = C [OK]
- Trying to import Node.js modules directly
- Assuming full rewrite is always needed
- Ignoring IPC as a complex alternative
Ready to prove it?
Pass the test and this pattern moves unknown
