The Wild

Migrate from Node.js to Deno: Step-by-Step

· 5 min read · Mammals

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide walks you through migrating an existing Node.js project to Deno, covering dependency changes, module resolution, TypeScript handling, and API differences. By the end, you'll have a working Deno application with minimal friction.

What You'll Need

  • Deno installed (latest stable version) — see deno.land for installation instructions.
  • Your existing Node.js project source code.
  • A basic understanding of JavaScript/TypeScript and command line usage.

Step-by-Step

  1. Assess your project's dependencies. List all packages from your package.json. Deno uses URL-based imports or the npm: specifier for Node packages, but not node_modules or require(). For each dependency, check if it has a Deno-compatible version or a native Deno alternative.
  2. Set up a deno.json configuration file. Create deno.json in your project root. This file can hold import maps, compiler options, and task definitions. For example, you can define an import map to alias common packages:
{
  "imports": {
    "express": "npm:express@5",
    "lodash": "npm:lodash@4"
  }
}
  1. Convert require() to import statements. Replace all CommonJS require() calls with ES module import statements. For default exports, use import express from 'express'. For named exports, use import { readFile } from 'fs/promises'. Deno supports ES modules natively.
  2. Update module specifiers. In Node.js, you import built-in modules like fs or path without a prefix. In Deno, you must prefix them with node:, e.g., import { readFile } from 'node:fs/promises'. For npm packages, use the npm: specifier in the import map or directly, e.g., import express from 'npm:express'.
  3. Handle TypeScript configuration. Deno supports TypeScript out of the box. Remove tsconfig.json or adapt it to Deno's compiler options. Deno uses a strict, isolated module system; you may need to adjust strict flags. Deno respects deno.json compiler options, not tsconfig.json.
  4. Replace Node.js global objects. Some Node.js globals like process and Buffer are not available in Deno by default. Use Deno.env for environment variables, Deno.args for command-line arguments, and Uint8Array or TextEncoder for binary data. For process.cwd(), use Deno.cwd().
  5. Adapt file system operations. Deno's file system API is promise-based and similar to Node.js, but you need to import from node:fs or use Deno's native API. For example, Deno.readTextFile() is a simple replacement for fs.readFile with encoding. Be aware of permission prompts — Deno requires explicit --allow-read, --allow-write, etc., flags.
  6. Update HTTP server code. If you use Express, you can often keep it via the npm: specifier. However, Deno's native HTTP server (Deno.serve) is more performant. Consider rewriting simple servers to use Deno.serve for a native feel. For example:
Deno.serve((req) => new Response('Hello, Deno!'));
  1. Adjust for Deno's permission system. Deno is secure by default. You must explicitly grant permissions via CLI flags or a config file. For example, to allow network access, run deno run --allow-net main.ts. Update your scripts and documentation accordingly.
  2. Migrate test files. Deno has a built-in test runner (deno test). Replace Node.js test frameworks like Jest or Mocha with Deno's Deno.test(). For example:
Deno.test("my test", () => { /* assertions */ });
  1. Update package scripts. Replace npm run start with deno run --allow-net main.ts. You can define tasks in deno.json under tasks for convenience.
  2. Run and debug. Execute deno run main.ts and fix any errors. Common issues include missing permissions, incompatible APIs, and type errors. Use deno lint and deno fmt to clean up code.

Common Mistakes

  • Forgetting to add node: prefix to built-in modules.
  • Using process or Buffer without adaptation.
  • Not granting permissions, leading to runtime errors.
  • Assuming require() works — it doesn't; use import.
  • Leaving package.json scripts that call node — update to deno.
  • Not updating import paths for npm packages — use npm: specifier.

Tips

  • Use an import map to centralize dependency versions and make imports cleaner.
  • Start with a small module or a single file to test the migration before tackling the whole project.
  • Leverage Deno's built-in TypeScript support — no separate compilation step needed.
  • Use deno doc to generate documentation from your code.
  • For Node.js-specific APIs, check if Deno has a native alternative; often it's simpler and faster.
  • Run deno check to type-check your project without running it.

Troubleshooting

  • Module not found errors: Ensure you have the correct specifier — for npm packages, use npm: prefix; for built-ins, use node:. Check your import map.
  • Permission denied: Add the appropriate --allow-* flags. For example, --allow-read, --allow-write, --allow-net. You can also use --allow-all for development.
  • TypeScript compilation errors: Deno's type checking is stricter. Adjust your types or use // @ts-ignore sparingly. Check deno.json compiler options.
  • Global variable undefined: Replace process with Deno.env, Buffer with Uint8Array, and __dirname with import.meta.url.
  • Express not working: Some Node.js packages rely on Node.js-specific internals. Try a Deno-native framework like oak or hono if Express fails.
  • Tests failing: Update test imports and use Deno.test. For assertions, use assert from the standard library.

Frequently Asked Questions

Can I use npm packages in Deno?

Yes, Deno can import npm packages using the npm: specifier, e.g., import express from 'npm:express'. However, some packages may not work if they rely on Node.js-specific APIs that Deno doesn't support.

Do I need to rewrite all my code?

Not necessarily. If your code is modern ES modules, the migration is mostly about changing import paths and handling globals. CommonJS code will need conversion to ES modules.

How do I handle environment variables?

Use Deno.env.get('VAR') to read environment variables. You can also use --env-file flag to load a .env file.

Is Deno faster than Node.js?

In many cases, Deno has better performance for I/O and startup time due to its Rust core and V8 engine. However, performance depends on the workload.

Can I use Deno with existing CI/CD pipelines?

Yes, Deno can be installed in CI environments. You can use deno test and deno lint in place of Node.js commands.

What about TypeScript configuration?

Deno uses its own configuration via deno.json. You don't need a separate tsconfig.json, but you can specify compiler options in deno.json.