gotchatypescriptModerate
Deno vs Node.js compatibility with npm packages
Viewed 0 times
deno npm compatnpm: prefixnode: prefixCommonJSimport.meta.dirnamenodeModulesDir
deno
Error Messages
Problem
Migrating Node.js code to Deno or using npm packages in Deno projects faces compatibility issues with Node.js built-in modules and CommonJS.
Solution
Deno supports npm packages via npm: prefix (e.g., import express from 'npm:express'). Node built-ins work via node: prefix. Use 'nodeModulesDir: auto' in deno.json for full Node.js compat mode.
Why
Deno was originally ESM-only with URL imports. Node.js compatibility was added incrementally. Full compatibility requires explicit opt-in for some features.
Gotchas
- CJS packages imported via npm: are automatically converted but may have edge cases
- __dirname and __filename are not available by default — use import.meta.dirname
- process.env works but must be granted --allow-env permission
- Some npm packages that rely on native Node.js add-ons (.node files) will not work in Deno
Code Snippets
Importing npm and Node packages in Deno
import express from 'npm:express@4';
import { readFileSync } from 'node:fs';
import { dirname } from 'node:path';Revisions (0)
No revisions yet.