Skip to content

About

Cloudflared in Node. Create HTTPS tunnel in 10 seconds. Works on macOS, Linux, and Windows.

Topics

Resources

Stars

104 stars

Watchers

1 watching

Forks

Repository files navigation

cloudflared

A Node.js package that allows you to easily create HTTPS tunnels using Cloudflare's cloudflared command-line tool. It provides a typed API for creating tunnels and managing the cloudflared binary installation.

Installation

You can install this package using your favorite package manager:

PNPM

pnpm i -g cloudflared

NPM

npm i -g cloudflared

Yarn

yarn global add cloudflared

The npm package includes the compiled JavaScript CLI, library, and type declarations. For a project dependency, run npm install cloudflared.

The CLI downloads a missing binary on first run. To download it during setup, run cloudflared bin install for a global install or npm exec -- cloudflared bin install for a project dependency. Set CLOUDFLARED_VERSION to choose an exact release. The default is the latest Cloudflare release.

The CLI reuses an existing executable by default. Set CLOUDFLARED_UPDATE_INTERVAL to a positive interval in milliseconds to check for updates at CLI startup. For example, 2592000000 checks at most once every 30 days. This setting applies to the package's default binary path. The CLI runs an existing CLOUDFLARED_BIN override directly.

Upgrading from a release with postinstall

If your application relied on package installation to download the executable, call await install(bin, {}) before creating a tunnel or installing a service. You can also provide an existing executable with CLOUDFLARED_BIN or use(path) for the library.

If the runtime has no network access or the installed package is read-only, run cloudflared bin install while building the deployment or provision the executable separately. Ship the executable with the application, keep it executable, and set CLOUDFLARED_BIN to its absolute path in the runtime environment. Use an executable for the runtime's operating system and architecture. Leave periodic updates disabled for an offline deployment.

When working in a source checkout, run pnpm install and pnpm build. Run the local CLI with node lib/cloudflared.js.

CLI Usage

You can use the cloudflared command-line tool to create HTTPS tunnels. You can find the usage of cloudflared here.

In addition to the standard cloudflared commands, this package also provides an extra subcommand: cloudflared bin. You can use it to manage the cloudflared binary version.

❯ cloudflared bin --help
cloudflared bin                    : Prints the path to the binary
cloudflared bin remove             : Removes the binary
cloudflared bin install [version]  : Installs the binary
cloudflared bin list               : Lists 30 latest releases
cloudflared bin help               : Prints this help message
Examples:
cloudflared bin install            : Installs the latest version of cloudflared
cloudflared bin install 2023.4.1   : Installs cloudflared 2023.4.1
You can find releases at https://github.com/cloudflare/cloudflared/releases

Library Usage

You can also use it as a library in your TypeScript / JavaScript projects.

Binary Path & Install

Prepare the managed executable before calling the library. bin is its path, and install with an options object can reuse an existing executable.

import { bin, install } from "cloudflared";
import { spawn } from "node:child_process";

await install(bin, {});
spawn(bin, ["--version"], { stdio: "inherit" });

If you supply your own executable, set CLOUDFLARED_BIN before loading the package or call use(path) before creating a Tunnel or installing a service.

Automatic updates

Library consumers can pass options to install before spawning the binary or creating a Tunnel. Synchronous Tunnel constructors do not check for updates.

import { bin, install } from "cloudflared";
import { spawn } from "node:child_process";

await install(bin, {
  minimumVersion: "2026.10.2",
  checkInterval: 30 * 24 * 60 * 60 * 1000,
});
spawn(bin, ["--version"], { stdio: "inherit" });
  • minimumVersion: Upgrade an older binary immediately, even if a periodic check is not due. Missing or older binaries install the latest release, unless a version is pinned.
  • checkInterval: Enable periodic updates to the newest version by specifying the period in milliseconds.

CLOUDFLARED_VERSION pins take precedence over periodic updates. The library's install options enforce minimumVersion and reject a pin below that version.

An exact version can be installed with install(path, version). This form and install(path) reinstall the binary even if it is already present. Pass an options object to reuse a suitable installed binary.

Installs targeting the same binary take turns replacing it and wait up to six seconds for the installation lock. A later installer can recover an abandoned lock when the operating system confirms the owner has exited. Recovery assumes the installers share a local filesystem, host, and process ID namespace.

Tunnel

Checkout examples/tunnel.mjs (or examples/tunnel.js for CommonJS).

Tunnel is inherited from EventEmitter, so you can listen to the events it emits, checkout examples/events.mjs.

import { Tunnel, bin, install } from "cloudflared";

console.log("Cloudflared Tunnel Example.");
main();

async function main() {
  await install(bin, {});

  // run: cloudflared tunnel --hello-world
  const tunnel = Tunnel.quick();

  // show the url
  const url = new Promise((resolve) => tunnel.once("url", resolve));
  console.log("LINK:", await url);

  // wait for connection to be established
  const conn = new Promise((resolve) => tunnel.once("connected", resolve));
  console.log("CONN:", await conn);

  // stop the tunnel after 15 seconds
  setTimeout(tunnel.stop, 15_000);

  tunnel.on("exit", (code) => {
    console.log("tunnel process exited with code", code);
  });
}
❯ node examples/tunnel.mjs
Cloudflared Tunnel Example.
LINK: https://mailto-davis-wilderness-facts.trycloudflare.com
CONN: {
  id: 'df1b8330-44ea-4ecb-bb93-8a32400f6d1c',
  ip: '198.41.200.193',
  location: 'tpe01'
}
tunnel process exited with code 0

Service

Prepare the executable before calling service.install(token), just as you would before creating a tunnel.

Checkout examples/service.mjs (or examples/service.js for CommonJS).

import { service } from "cloudflared";

console.log("Cloudflared Service Example.");
main();

async function main() {
  if (service.exists()) {
    console.log("Service is running.");
    const current = service.current();
    for (const { service, hostname } of current.config.ingress) {
      console.log(`  - ${service} -> ${hostname}`);
    }
    console.log("metrics server:", current.metrics);
  } else {
    console.log("Service is not running.");
  }
}
❯ node examples/service.mjs
Cloudflared Service Example.
Service is running.
  - http://localhost:12345 -> sub.example.com
  - http_status:404 -> undefined
metrics server: 127.0.0.1:49177/metrics

NOTICE: On linux, service can only be installed and uninstalled by root.

Run service test on linux: sudo -E env "PATH=$PATH" pnpm test

About

Cloudflared in Node. Create HTTPS tunnel in 10 seconds. Works on macOS, Linux, and Windows.

Topics

Resources

Stars

104 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages