How to Use node: Specifier in Deno for Node.js Compatibility
In Deno, you can use the
node: specifier to import Node.js built-in modules directly, like import fs from "node:fs". This allows you to write code compatible with Node.js APIs while running in Deno. Make sure to enable the --unstable flag and have the --compat mode enabled if needed.Syntax
The node: specifier is used as a prefix in import statements to load Node.js built-in modules in Deno. It tells Deno to resolve the module as a Node.js core module.
import <name> from "node:<module>";- imports the Node.js built-in module named<module>.<name>- the variable name you assign the imported module to.<module>- the Node.js core module name, likefs,path, orcrypto.
This syntax is similar to Node.js ES module imports but works in Deno with compatibility enabled.
typescript
import fs from "node:fs"; import path from "node:path"; console.log(typeof fs.readFileSync); // function console.log(typeof path.join); // function
Example
This example shows how to import the Node.js fs and path modules using the node: specifier in Deno. It reads the current file's content and prints the file name using path.basename.
typescript
import fs from "node:fs"; import path from "node:path"; const filePath = new URL(import.meta.url).pathname; const content = fs.readFileSync(filePath, "utf-8"); const fileName = path.basename(filePath); console.log(`File name: ${fileName}`); console.log(`Content length: ${content.length}`);
Output
File name: example.ts
Content length: 200
Common Pitfalls
Common mistakes when using the node: specifier in Deno include:
- Not running Deno with the
--unstableflag, which is required for Node.js compatibility features. - Forgetting to enable
--compatmode to allow Node.js built-in modules to work properly. - Using Node.js modules that rely on native binaries or APIs not supported by Deno's compatibility layer.
- Trying to import Node.js modules without the
node:prefix, which will fail in Deno.
Example of wrong and right import:
typescript
/* Wrong: Missing node: prefix */ // import fs from "fs"; // This will fail in Deno /* Right: Using node: prefix */ import fs from "node:fs"; console.log(typeof fs.readFileSync); // function
Output
function
Quick Reference
| Usage | Description |
|---|---|
| import mod from "node: | Import Node.js built-in module in Deno |
| --unstable flag | Required to enable Node.js compatibility features |
| --compat flag | Enables Node.js compatibility mode in Deno |
| Use node: prefix | Must prefix Node.js core modules with node: |
| Not all modules supported | Some Node.js modules may not work due to native dependencies |
Key Takeaways
Use the node: prefix to import Node.js built-in modules in Deno.
Run Deno with --unstable and --compat flags to enable Node.js compatibility.
Not all Node.js modules work in Deno due to native or unsupported APIs.
Always use import statements like import fs from "node:fs"; in Deno for Node.js modules.
Check Deno documentation for the latest support and compatibility details.