
Node.js 20: Run Node.js without installing it
19 April, 2023
9
9
1
Contributors
Node.js is a popular open-source JavaScript runtime environment that allows developers to create scalable and performant web applications. Node.js has a large and active community that contributes to its development and improvement. Every six months, a new version of Node.js is released with new features and enhancements. In this blog post, we will explore some of the highlights of Node.js 20, released on April 18th, 2023.
How to install Node.js 20
To download Node.js 20.0.0, visit its official website. You can find the release post on their blog, which contains the full list of commits included in this release.
Please make a note that Node.js 20 is still in the "current" version. It will be available for long-term support(LTS) in October. The community is still experimenting with most of the features mentioned in the blog, so there might be some changes in the LTS version. I
On a side note, Node.js 14 will go End-of-Life in April 2023. Therefore, start planning to upgrade to Node.js 18 (LTS) or Node.js 20, which will become LTS six months later.
Run Node.js without installing it
This is the most exciting feature of all. With this latest feature, you can execute Node.js on systems that do not have Node.js installed. You can run single executable apps from your Node.js projects.
Node.js creates single executable applications by injecting a Node.js prepared blob. The blob contains a bundled script into the `node` binary. The program checks if anything has been injected during start up. If the blob is detected, it executes the script in the blob. If not, the program runs like it usually would. Users can create a single executable application from their bundled script with the node binary itself and any tool which can inject resources into the binary.
Note: This feature only supports running a single embedded script using the CommonJS module system.
This can make it easier to distribute and deploy your Node.js applications, as well as improve their startup time and security. It is tested regularly on CI on Windows and macOS. For Linux, it supports all distros supported by Node.js except Alpine and all architectures supported by Node.js except s390x and ppc64).
Read more about single Executable Applications
Experimental Permission Model
The experimental permission model is one of the most notable additions to Node.js 20. This feature allows developers to control what resources a Node.js process can access during execution. For example, you can restrict a Node.js process from accessing the file system, child process, or worker threads.
To enable the permission model, you must use the --experimental-permission
flag when launching Node.js. When you do so, the process can't access the file system, spawn processes, and use node:worker_threads
.
Developers now have more control over file system access. Two new experimental flags, --allow-fs-read
and --allow-fs-write
allow more restricted control over which parts of the file system a process can access. To enable these flags, developers must pair them with the experimental-permission
flag.
For example, to run a process to allow both read and write access to the entire file system:
node --experimental-permission --allow-fs-read=* --allow-fs-write=* index.js
You can also use specific directories or wildcard patterns to allow particular access at a single execution. This can improve the security and reliability of your Node.js applications, especially when running untrusted code or third-party modules.
Read more abbout Permission Model
Stable test_runner module
Another important change Node.js 20 brings is that the test_runner
module is now marked as stable. The test_runner module is a built-in module that provides a simple and lightweight framework for writing and running tests in Node.js. It was introduced as an experimental feature in Node.js 18 but has been marked as stable in Node.js 20.
The stable test_runner includes building blocks for authoring and running tests. The test_runner module allows you to write tests using assertions, hooks, and reporters. You can also run your tests in parallel or serial mode and generate code coverage reports.
Here's how you can use test_runner, according to the blog:
import { test, mock } from 'node:test';
import assert from 'node:assert';
import fs from 'node:fs';
mock.method(fs, 'readFile', async () => "Hello World");
test('synchronous passing test', async (t) => {
// This test passes because it does not throw an exception.
assert.strictEqual(await fs.readFile('a.txt'), "Hello World");
});
Updates of v8 JavaScript engine
Node.js 20 also comes with an updated version of the V8 JavaScript engine, which is responsible for executing JavaScript code in Node.js. The V8 version in Node.js 20 is 11.3, which brings several improvements and bug fixes. Some of the notable changes include:
- String.prototype.isWellFormed and toWellFormed
- Methods that change Array and TypedArray by copy
- Resizable ArrayBuffer and growable SharedArrayBuffer
- RegExp v flag with set notation + properties of strings
- WebAssembly Tail Call
Other noteworthy changes
- Node.js 20 officially includes binaries for ARM64 Windows architecture. This allows native execution on Windows platforms.
- The team is progressing on Web Assembly System Interface implementation within Node.js. Developers don't need to command line option to consume WASI.
- Node.js is working toward interoperability with other JS environments. One example is Crypto API functions' arguments are now coerced and validated as per their WebIDL definitions like in other Web Crypto API implementations.
- The latest version also introduced the synchronous
import.meta.resolve
function. It allows you to resolve a module specifier relative to a given module URL. This can be useful when you want to dynamically import modules using import() or require(). - Node.js 20 also comes with the latest version of
Ada
, a URL parser. The update brings performance improvements to URL parsing. The latest version performs better than its predecessor while eliminating the need for the ICU requirement for URL hostname parsing.
Closing Notes
Node.js brings a lot of exciting features to the table with its latest release of Node.js 20. The ability to execute programs without needing Node.js in the system, stable test_runner, the newest version of Ada, Web Crypto API, and other improvements would surely ease the developer's life.
Node.js is constantly evolving and improving to meet the needs and expectations of developers. If you want to try out Node.js 20, you can download it from the official website or use a version manager like NVM. You can also check out the official documentation and changelog for more details and examples. Let us know what you think about Node.js 20 in the comments below. Happy coding!
javascript
nodejs
server