Migrate from Node.js to Deno: Step-by-Step
In this article
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
- Assess your project's dependencies. List all packages from your
package.json. Deno uses URL-based imports or thenpm:specifier for Node packages, but notnode_modulesorrequire(). For each dependency, check if it has a Deno-compatible version or a native Deno alternative. - Set up a
deno.jsonconfiguration file. Createdeno.jsonin 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"
}
}
- Convert
require()toimportstatements. Replace all CommonJSrequire()calls with ES moduleimportstatements. For default exports, useimport express from 'express'. For named exports, useimport { readFile } from 'fs/promises'. Deno supports ES modules natively. - Update module specifiers. In Node.js, you import built-in modules like
fsorpathwithout a prefix. In Deno, you must prefix them withnode:, e.g.,import { readFile } from 'node:fs/promises'. For npm packages, use thenpm:specifier in the import map or directly, e.g.,import express from 'npm:express'. - Handle TypeScript configuration. Deno supports TypeScript out of the box. Remove
tsconfig.jsonor adapt it to Deno's compiler options. Deno uses a strict, isolated module system; you may need to adjuststrictflags. Deno respectsdeno.jsoncompiler options, nottsconfig.json. - Replace Node.js global objects. Some Node.js globals like
processandBufferare not available in Deno by default. UseDeno.envfor environment variables,Deno.argsfor command-line arguments, andUint8ArrayorTextEncoderfor binary data. Forprocess.cwd(), useDeno.cwd(). - Adapt file system operations. Deno's file system API is promise-based and similar to Node.js, but you need to import from
node:fsor use Deno's native API. For example,Deno.readTextFile()is a simple replacement forfs.readFilewith encoding. Be aware of permission prompts — Deno requires explicit--allow-read,--allow-write, etc., flags. - 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 useDeno.servefor a native feel. For example:
Deno.serve((req) => new Response('Hello, Deno!'));
- 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. - Migrate test files. Deno has a built-in test runner (
deno test). Replace Node.js test frameworks like Jest or Mocha with Deno'sDeno.test(). For example:
Deno.test("my test", () => { /* assertions */ });
- Update package scripts. Replace
npm run startwithdeno run --allow-net main.ts. You can define tasks indeno.jsonundertasksfor convenience. - Run and debug. Execute
deno run main.tsand fix any errors. Common issues include missing permissions, incompatible APIs, and type errors. Usedeno lintanddeno fmtto clean up code.
Common Mistakes
- Forgetting to add
node:prefix to built-in modules. - Using
processorBufferwithout adaptation. - Not granting permissions, leading to runtime errors.
- Assuming
require()works — it doesn't; useimport. - Leaving
package.jsonscripts that callnode— update todeno. - 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 docto 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 checkto 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, usenode:. Check your import map. - Permission denied: Add the appropriate
--allow-*flags. For example,--allow-read,--allow-write,--allow-net. You can also use--allow-allfor development. - TypeScript compilation errors: Deno's type checking is stricter. Adjust your types or use
// @ts-ignoresparingly. Checkdeno.jsoncompiler options. - Global variable undefined: Replace
processwithDeno.env,BufferwithUint8Array, and__dirnamewithimport.meta.url. - Express not working: Some Node.js packages rely on Node.js-specific internals. Try a Deno-native framework like
oakorhonoif Express fails. - Tests failing: Update test imports and use
Deno.test. For assertions, useassertfrom 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.