Node.js Compatibility in Deno

Rate this content
Bookmark
Slides

Can Deno run apps and libraries authored for Node.js? What are the tradeoffs? How does it work? What’s next?

FAQ

Deno is a command-line runtime for JavaScript and TypeScript, similar to Node.js, but it supports TypeScript as a first-class citizen without requiring a separate build chain. Unlike Node.js, Deno utilizes secure execution by default, meaning programs can't access the file system, environmental variables, or the internet unless explicitly permitted.

Yes, Deno can execute Node.js applications and libraries using the --compat flag for compatibility and the --unstable flag due to some API limitations. However, not all Node.js APIs are fully supported yet, as Deno is still working on providing compatible APIs and polyfills.

Deno allows importing any third-party code using URLs directly, similar to browser ES module imports. This eliminates the need for a centralized package manager like npm, allowing code to be pulled from any public or private server. Deno fetches, caches, and compiles (or transpiles) this code transparently on the first run.

The main trade-offs include some Node.js APIs not being fully compatible or available in Deno, the need for additional flags to enable Node.js compatibility, and potential issues with native Node.js modules that rely on C++ APIs, which are not supported in Deno's Rust-based environment.

Deno emphasizes security by enforcing strict access controls. By default, Deno scripts run in a secure sandbox environment where access to system resources like the file system, environment variables, and network access is restricted unless explicitly granted via command-line flags.

While Deno offers many similar functionalities as Node.js and adds improvements like first-class TypeScript support and enhanced security, it is not fully compatible with the Node.js ecosystem, specifically with npm packages without adaptation. Developers can choose Deno for new projects but may find transitioning existing Node.js projects challenging without full compatibility.

Deno does not use a centralized package management system like npm. Instead, it directly imports modules from URLs, caching them locally after the first download. This approach aligns with web standards and allows more flexibility in sourcing modules from various providers.

Bartek Iwanczuk
Bartek Iwanczuk
34 min
17 Feb, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Deno aims to provide Node.js compatibility to make migration smoother and easier. While Deno can run apps and libraries offered for Node.js, not all are supported yet. There are trade-offs to consider, such as incompatible APIs and a less ideal developer experience. Deno is working on improving compatibility and the transition process. Efforts include porting Node.js modules, exploring a superset approach, and transparent package installation from npm.

Available in Español: Compatibilidad con Node.js en Deno

1. Introduction to Deno and Node.js Compatibility

Short description:

Hi, folks! My name is Bartek and I'm a member of the Deno core team. Today, we're going to discuss Node.js compatibility in Deno. Can Deno run apps and libraries offered for Node.js? What are the trade-offs? How does it all work and what is next on the plate for us?

Hi, folks! My name is Bartek and I'm a member of the Deno core team. Today, we're going to discuss Node.js compatibility in Deno. In this talk, I would like to give you a quick rundown of what is Deno, and try to answer a few questions regarding compatibility with Node.

So, can Deno run apps and libraries offered for Node.js? What are the trade-offs? How does it all work and what is next on the plate for us? So, let's get right to it!

Deno one-on-one. Deno is a new command-line runtime for JavaScript and for TypeScript. It's similar to Node.js, that you can execute your JavaScript code, but the difference is that we also support TypeScript as a first-class citizen. Word of caution here, Deno doesn't actually execute your TypeScript. It just transpiles it to JavaScript underneath, behind the scenes, so you don't need to set up a build chain or anything like that. Deno also aims to be fun and productive. We like to think of it as a bag of LEGO blocks.

2. Deno Execution and Node.js Compatibility

Short description:

Deno is a single, executable file that works on Linux, Mac, and Windows. It offers secure execution by default, preventing unauthorized access to file systems and internet connections. Deno allows linking to third-party code using URLs and fetches, caches, and compiles or transpiles the code transparently. While Deno is not initially compatible with npm, some users find the lack of npm support a hurdle for adoption.

Deno is a single, executable file. This file is all you need to run any Deno program, and we will always distribute Deno as such. Deno works on Linux, it works on Mac and on Windows, so wherever you go, new server, new laptop, whatever, there's only a single command you need to execute to get Deno and you're ready to go.

Deno has something we like to call secure execution by default. What that means is none of your programs can access your file system, your environmental variables, sponsor processes, or even open an internet connection unless you explicitly allow it to. In this short example we can also see that you can directly link to third-party code and you can execute it even though you don't trust this code. So, in here we are executing an example from DenoSTD, which is Deno's standard library, it's a set of modules that are guaranteed to work with Deno, but it could have been any URL in the world you can think of, and in this case where there are no flags provided besides run sub-commands, this program cannot do anything to your computer. But if you want to access a file system or spawn a sub-process or open an internet connection, you can give Deno some flags and tell it to allow your program to actually do these things. And as you can see here some of these flags allow you to scope this even further. So for example, we can allow our program to read from the file system, however only from the slash tmp directory. Or we can tell your program to allow open an internet connection, however, only scoped to google.com domain.

Deno allows you to link to any third-party code using URLs. It works just like it does in browser. In this example we can see our es module import. We are importing a magenta function, again from Deno STD. This time we are doing this from fmt slash colors module. So this module resolution is simple and compatible with browsers and Deno has no notion of node modules or indexJS. Or actually that's what this talk is about after all. Deno doesn't depend on a centralized server. That is, you are not explicitly tied only to npm if you want to link to your packages or install new packages or a private registry if you have one. You can pull code from anywhere on the internet, either public servers or private servers if you provide proper authorization headers. Deno as a runtime fetches, caches and compiles this code, or transpiles it, transparently for you on the first run. After the first run, the code is already in cache, so there is no need to download it again. In fact, Deno won't download the code again unless you explicitly ask it to.

So, why are we even thinking or working on Node.js compatibility? Well, npm is arguably the biggest package ecosystem in the world, and users just want to use this package ecosystem. When Deno first came out with v1.0, we stated explicitly that we are not compatible with npm. And that was fine. Here, some users still find lack of support for npm a huge hurdle for adoption. So some users want to try Deno instead of Node.js. However, the cost to switch from one runtime to the other often proves too high to even try and do it.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Node Congress 2022Node Congress 2022
26 min
It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Top Content
Do you know what’s really going on in your node_modules folder? Software supply chain attacks have exploded over the past 12 months and they’re only accelerating in 2022 and beyond. We’ll dive into examples of recent supply chain attacks and what concrete steps you can take to protect your team from this emerging threat.
You can check the slides for Feross' talk here.
Towards a Standard Library for JavaScript Runtimes
Node Congress 2022Node Congress 2022
34 min
Towards a Standard Library for JavaScript Runtimes
Top Content
You can check the slides for James' talk here.
ESM Loaders: Enhancing Module Loading in Node.js
JSNation 2023JSNation 2023
22 min
ESM Loaders: Enhancing Module Loading in Node.js
Native ESM support for Node.js was a chance for the Node.js project to release official support for enhancing the module loading experience, to enable use cases such as on the fly transpilation, module stubbing, support for loading modules from HTTP, and monitoring.
While CommonJS has support for all this, it was never officially supported and was done by hacking into the Node.js runtime code. ESM has fixed all this. We will look at the architecture of ESM loading in Node.js, and discuss the loader API that supports enhancing it. We will also look into advanced features such as loader chaining and off thread execution.
How Bun Makes Building React Apps Simpler & Faster
React Day Berlin 2022React Day Berlin 2022
9 min
How Bun Makes Building React Apps Simpler & Faster
Bun’s builtin JSX transpiler, hot reloads on the server, JSX prop punning, macro api, automatic package installs, console.log JSX support, 4x faster serverside rendering and more make Bun the best runtime for building React apps
Out of the Box Node.js Diagnostics
Node Congress 2022Node Congress 2022
34 min
Out of the Box Node.js Diagnostics
In the early years of Node.js, diagnostics and debugging were considerable pain points. Modern versions of Node have improved considerably in these areas. Features like async stack traces, heap snapshots, and CPU profiling no longer require third party modules or modifications to application source code. This talk explores the various diagnostic features that have recently been built into Node.
You can check the slides for Colin's talk here. 
Javascript Should Come With Batteries
React Day Berlin 2023React Day Berlin 2023
30 min
Javascript Should Come With Batteries
Setting up JavaScript projects is no fun. Getting started involves installing and configuring node, tsc, prettier, eslint, a testing framework, a database driver, and more. Why is JavaScript not batteries included? In this talk we'll talk about how Deno fixes this, letting you focus on building stuff. We explore what benefits full tooling integration unlocks, and remember how fun it is to program if your tools help you, rather than requiring your babysitting.

Workshops on related topic

Node.js Masterclass
Node Congress 2023Node Congress 2023
109 min
Node.js Masterclass
Top Content
Workshop
Matteo Collina
Matteo Collina
Have you ever struggled with designing and structuring your Node.js applications? Building applications that are well organised, testable and extendable is not always easy. It can often turn out to be a lot more complicated than you expect it to be. In this live event Matteo will show you how he builds Node.js applications from scratch. You’ll learn how he approaches application design, and the philosophies that he applies to create modular, maintainable and effective applications.

Level: intermediate
Build and Deploy a Backend With Fastify & Platformatic
JSNation 2023JSNation 2023
104 min
Build and Deploy a Backend With Fastify & Platformatic
WorkshopFree
Matteo Collina
Matteo Collina
Platformatic allows you to rapidly develop GraphQL and REST APIs with minimal effort. The best part is that it also allows you to unleash the full potential of Node.js and Fastify whenever you need to. You can fully customise a Platformatic application by writing your own additional features and plugins. In the workshop, we’ll cover both our Open Source modules and our Cloud offering:- Platformatic OSS (open-source software) — Tools and libraries for rapidly building robust applications with Node.js (https://oss.platformatic.dev/).- Platformatic Cloud (currently in beta) — Our hosting platform that includes features such as preview apps, built-in metrics and integration with your Git flow (https://platformatic.dev/). 
In this workshop you'll learn how to develop APIs with Fastify and deploy them to the Platformatic Cloud.
0 to Auth in an Hour Using NodeJS SDK
Node Congress 2023Node Congress 2023
63 min
0 to Auth in an Hour Using NodeJS SDK
WorkshopFree
Asaf Shen
Asaf Shen
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.JS backend + React frontend) to authenticate users with OAuth (social login) and One Time Passwords (email), including:- User authentication - Managing user interactions, returning session / refresh JWTs- Session management and validation - Storing the session for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.
Table of contents- A quick intro to core authentication concepts- Coding- Why passwordless matters
Prerequisites- IDE for your choice- Node 18 or higher
Building a Hyper Fast Web Server with Deno
JSNation Live 2021JSNation Live 2021
156 min
Building a Hyper Fast Web Server with Deno
WorkshopFree
Matt Landers
Will Johnston
2 authors
Deno 1.9 introduced a new web server API that takes advantage of Hyper, a fast and correct HTTP implementation for Rust. Using this API instead of the std/http implementation increases performance and provides support for HTTP2. In this workshop, learn how to create a web server utilizing Hyper under the hood and boost the performance for your web apps.
Build Peer-to-Peer Applications with Pear Runtime
JSNation 2024JSNation 2024
152 min
Build Peer-to-Peer Applications with Pear Runtime
WorkshopFree
David Mark Clements
David Mark Clements
Learn how to rapidly build peer-to-peer applications with Pear Runtime. No servers required. Understand peer-to-peer paradigms and construct applications from well-defined building blocks. This workshop will cover how to create both Desktop and Terminal applications (with discussion for Mobile) that work entirely peer-to-peer from anywhere in the world. By the end of this workshop you should know how to build a new type of highly scalable application with entirely reduced infrastructural costs (~0) along with suitable architectures and best practices for peer-to-peer applications. From the creator of Pear Runtime and the company that brings us keet.io. Table of content:- Introducing Pear- Initial Q & A- Getting Setup- Creating a Pear Desktop Application- Sharing a Pear Application- Running a Pear Application- Creating a Pear Terminal Application- Releasing a Pear Application- Architectural Discussions- Wrap-up Q & A
GraphQL - From Zero to Hero in 3 hours
React Summit 2022React Summit 2022
164 min
GraphQL - From Zero to Hero in 3 hours
Workshop
Pawel Sawicki
Pawel Sawicki
How to build a fullstack GraphQL application (Postgres + NestJs + React) in the shortest time possible.
All beginnings are hard. Even harder than choosing the technology is often developing a suitable architecture. Especially when it comes to GraphQL.
In this workshop, you will get a variety of best practices that you would normally have to work through over a number of projects - all in just three hours.
If you've always wanted to participate in a hackathon to get something up and running in the shortest amount of time - then take an active part in this workshop, and participate in the thought processes of the trainer.