Writing universal modules for Deno, Node, and the browser

Rate this content
Bookmark

This talk will walk you through writing a module in TypeScript that can be consumed by users of Deno, Node, and browsers. I will walk through how to set up formatting, linting, and testing in Deno, and then how to publish your module to deno.land/x and npm. I will also start out with a quick introduction on what Deno is.

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

FAQ

Luka is a member of the Deno team at the Deno company. He works on the Deno CLI, Deno Deploy, and participates in Web Standards work, including contributions to TC39, W3C, and whatwg specifications.

Deno is a modern runtime for TypeScript and JavaScript. It runs TypeScript out of the box without needing transpilation, includes built-in utilities like linting and formatting, is secure by default, and follows web standards closely.

Deno is secure by default. Similar to the browser, Deno restricts access to files, environment variables, and network resources unless explicitly granted by the user.

Deno offers several built-in utilities including denolint for linting, deno fmt for formatting code, a test framework, editor integrations, and documentation generation tools.

Deno runs TypeScript out of the box with zero configuration. You can import and run .ts and .tsx files directly without any additional setup.

Deno Deploy is a hosted cloud offering by the Deno company that allows you to run Deno projects globally, close to users at the edge.

To run Deno projects in VS Code, install the Deno extension from the marketplace, initialize workspace configuration using the command palette, and enable linting and other settings as needed.

DNT (Deno Node Transform) is a build tool that converts Deno TypeScript code to CommonJS and ECMAScript modules for use in Node. It handles polyfills, transpiles tests, and ensures compatibility between Deno and Node environments.

Deno uses ECMAScript modules, import maps for dependency remapping, and follows web standards. It does not have a custom HTTP API but uses the Fetch API like in the browser, and it also supports WebWorkers and web streams.

You can publish a Deno module to any web server or use the Deno module registry at deno.land/x, which offers immutability and GitHub integration. You can also use DNT to publish your module to NPM for Node compatibility.

Luca Casonato
Luca Casonato
25 min
29 Apr, 2022

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Deno is a modern runtime for TypeScript and JavaScript that runs out of the box and is secure by default. The Talk covers building a greeting message library with Deno, compiling Deno libraries for Node consumers, setting up the editor and writing code, writing tests and running them with dnotest, code formatting, linting, and publishing, publishing to NPM and running tests, and building and publishing the NPM package. The speaker emphasizes the ease of use and integration of Deno's tooling system.

1. Introduction to Deno and TypeScript

Short description:

I'm Luka from the Deno team. I work on Deno and Deno Deploy. Deno is a modern runtime for TypeScript and JavaScript. Deno runs TypeScript out of the box. It's secure by default. Deno is a single executable. It has a big standard library of modules. We aim to encapsulate the top 100 or something of npm modules.

Hey folks, I'm Luka from the Deno team and I'm going to be talking to you today about writing TypeScript code for Deno, Node, and the browser. Quickly about me, who am I? I am Luka. I work on the Deno team at the Deno company on Deno, the Deno CLI, so that's the open source tool that you can download and run on your own computer. And I also work on Deno Deploy, which is our hosted cloud offering that lets you run Deno projects all across the world close to users at the edge. You can learn more about that at deno.com.

2. Introduction to Deno and its Features

Short description:

I do a lot of Web Standards work, sit on TC39 as a delegate, contribute to W3C and whatwig specifications. Deno is a modern runtime for TypeScript and JavaScript. Deno runs TypeScript out of the box, no transpilation needed. It has built-in utilities, great editor integrations, and a big standard library. Deno is secure by default, single executable, and follows web standards.

And the other thing I do is do a lot of Web Standards work, so I sit on TC39 as a delegate, TC39 is the standards committee that develops JavaScript, the language, and I also contribute to some W3C and whatwig specifications, so things like fetch, the streams API, things like that is sort of the things I open issues on, or write PRs to, or write tests for.

Yeah, so that's me. Then for all of you who aren't familiar with what Deno is, let me give you a quick rundown. Deno is a modern runtime for TypeScript and JavaScript. Usually, we say the other way around, JavaScript and TypeScript, but this is TypeScript Congress, so it's for TypeScript and JavaScript.

Yeah, so what you're probably most interested in is that Deno runs TypeScript out of the box. It can run .ts files and .tsx files out of the box, no transpilation needed. You just import them and they run. It has a bunch of built-in utilities that work great with TypeScript and JavaScript. So denolint does some linting, denoformatting to format your code just like it would with Prettier. We have a test framework built in. We have great editor integrations which I'll showcase later. Documentation generation, there's a bunch more.

Something else that Deno provides is that it's secure by default. So just like the browser, you can't just do anything without the users consenting. So you can't read files from disk, you can't read environment variables, you can't talk the network without the user explicitly allowing that. So that can either happen by... Yeah, like if you're in the browser the site can't just send you notifications but you have to explicitly opt-in. Deno is also single executable that you download. So unlike Node or some other project it's not a whole zip file of different files that you need to put somewhere, but it's just a single executable that you place somewhere on your path and it runs. There's no need to install OpenSSL. Deno also has a big standard library of modules which are very useful for day-to-day development. Things like YAML encoder, Base64 encoder, cryptography things, HTTP servers, you name it, it's in there. And we aim to encapsulate the top 100 or something of npm modules. We also try to very closely follow web standards where possible. So Deno does not have a custom HTTP API. Instead we just use the Fetch API just like you would in the browser. We also use import maps for dependency remapping. Just like in the browser, we use ECMAScript modules, we use WebWorkers for multithreading, we use web streams for anything that's streaming and promises for anything that's asynchronous. So there's no callbacks, there's no custom streams implementation, it's all very standard.

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.
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.
Node.js Compatibility in Deno
Node Congress 2022Node Congress 2022
34 min
Node.js Compatibility in Deno
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.
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
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.
Mastering Node.js Test Runner
TestJS Summit 2023TestJS Summit 2023
78 min
Mastering Node.js Test Runner
Workshop
Marco Ippolito
Marco Ippolito
Node.js test runner is modern, fast, and doesn't require additional libraries, but understanding and using it well can be tricky. You will learn how to use Node.js test runner to its full potential. We'll show you how it compares to other tools, how to set it up, and how to run your tests effectively. During the workshop, we'll do exercises to help you get comfortable with filtering, using native assertions, running tests in parallel, using CLI, and more. We'll also talk about working with TypeScript, making custom reports, and code coverage.