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?

This talk has been presented at Node Congress 2022, check out the latest edition of this Tech Conference.

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
The talk discusses the importance of supply chain security in the open source ecosystem, highlighting the risks of relying on open source code without proper code review. It explores the trend of supply chain attacks and the need for a new approach to detect and block malicious dependencies. The talk also introduces Socket, a tool that assesses the security of packages and provides automation and analysis to protect against malware and supply chain attacks. It emphasizes the need to prioritize security in software development and offers insights into potential solutions such as realms and Deno's command line flags.
Towards a Standard Library for JavaScript Runtimes
Node Congress 2022Node Congress 2022
34 min
Towards a Standard Library for JavaScript Runtimes
Top Content
There is a need for a standard library of APIs for JavaScript runtimes, as there are currently multiple ways to perform fundamental tasks like base64 encoding. JavaScript runtimes have historically lacked a standard library, causing friction and difficulty for developers. The idea of a small core has both benefits and drawbacks, with some runtimes abusing it to limit innovation. There is a misalignment between Node and web browsers in terms of functionality and API standards. The proposal is to involve browser developers in conversations about API standardization and to create a common standard library for JavaScript runtimes.
ESM Loaders: Enhancing Module Loading in Node.js
JSNation 2023JSNation 2023
22 min
ESM Loaders: Enhancing Module Loading in Node.js
ESM Loaders enhance module loading in Node.js by resolving URLs and reading files from the disk. Module loaders can override modules and change how they are found. Enhancing the loading phase involves loading directly from HTTP and loading TypeScript code without building it. The loader in the module URL handles URL resolution and uses fetch to fetch the source code. Loaders can be chained together to load from different sources, transform source code, and resolve URLs differently. The future of module loading enhancements is promising and simple to use.
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 is a modern all-in-one JavaScript runtime environment that achieves new levels of performance. It includes BUN dev, a fast front-end dev server, BUN install, a speedy package manager, and BUN run, a fast package runner. BUN supports JSX, has optimized React server-side rendering, and offers hot module reloading on the server. The priorities for BUN include stability, node compatibility, documentation improvement, missing features in BUN install, AST plugin API, native Windows support, Bundler and Minifier optimization, and easier deployment to production. BUN's AST plugin API allows for bundle-time JavaScript execution and embedding code, potentially inspiring new frameworks.
Out of the Box Node.js Diagnostics
Node Congress 2022Node Congress 2022
34 min
Out of the Box Node.js Diagnostics
This talk covers various techniques for getting diagnostics information out of Node.js, including debugging with environment variables, handling warnings and deprecations, tracing uncaught exceptions and process exit, using the v8 inspector and dev tools, and generating diagnostic reports. The speaker also mentions areas for improvement in Node.js diagnostics and provides resources for learning and contributing. Additionally, the responsibilities of the Technical Steering Committee in the TS community are discussed.
Javascript Should Come With Batteries
React Day Berlin 2023React Day Berlin 2023
30 min
Javascript Should Come With Batteries
Watch video: Javascript Should Come With Batteries
JavaScript Should Come With Batteries: Deno is a next-generation JavaScript runtime that addresses the lack of built-in tooling in JavaScript. It provides a secure and simple way to develop applications with built-in testing, linting, formatting, and a language server for VS Code. Deno is compatible with Node.js and NPM, supports web standard APIs, and allows code portability between frontend and server. It also offers features like a built-in database, a key-value store, and transparent monetization with Deno Deploy.

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.
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.
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
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.