Back to blog

Follow and Subscribe

Fastly CLI on npm: now at your JavaScript fingertips

Katsuyuki Omuro

Senior Software Engineer, Developer Relations, Fastly

The Fastly CLI is the recommended tool provided by Fastly for interacting with the Fastly API from the command line. It’s an open-source tool used by developers and in continuous integration pipelines to perform various actions on behalf of a Fastly account, including creating services, managing backends and domains, and deploying Compute packages. If you’re working with Fastly Compute in JavaScript, we have exciting news that brings the Fastly CLI closer to you – the Fastly CLI is now available as a package on npm.

It’s been an exciting three years since we announced JavaScript support in the Fastly Compute edge platform, and almost two years since we released v1.0 of the JavaScript SDK. It’s a very popular language on the platform and also happens to be my personal favorite. Developing for Fastly Compute using JavaScript enables the creation of edge applications in a quick and fun way, and we want to extend access to it to as many users as we can.

As a developers-first company, there is one thing we always have on our minds: to remove as many obstacles as possible from getting in the way of actual development. That is, how can we enable developers to get the job done with one fewer click, with one fewer dependency, with one fewer tool needed to be installed? Is there a way to make JavaScript development on Compute simpler? That’s what we set out to tackle this time.

The Fastly CLI is an open-source tool used to perform actions with your Fastly account. Since it’s used for running and publishing Compute applications, it is one of the requirements for developing for Fastly Compute in JavaScript, even if it’s just to try it out locally. This has traditionally meant a trip to the GitHub repository to obtain a prebuilt package, or installing it through Homebrew on macOS. While these are steps that are generally familiar to developers, we wanted to lower the barrier as much as we can for getting your feet wet in Compute application development.

That’s why starting with version 10.14.0, we’ve decided to publish the Fastly CLI on npmjs.org as an additional avenue of distribution. If you’re a JavaScript developer in 2024, chances are you’re familiar with npmjs.org as the de facto package repository from where you install packages into your application, whether you use yarn, pnpm, or the trusty old npm as your interface. This means npmjs.org is available by default to everyone, making it a great way for us to put this essential tool into our users’ hands.

Run the Fastly CLI without needing to install it

One handy feature of npm is npx, which allows you to run commands from npm packages without being required to install them to a project. So long as Node.js and npm are available in your environment, you can now directly invoke the Fastly CLI as such:

npx @fastly/cli 

The first time you do this, you will be prompted by npm to fetch the package; this will add it to your environment’s npm cache, and it will be available immediately in the future.

Since the Fastly CLI is always called with additional parameters, you just specify them as normal as parameters following the command. For example, to list the services in your Fastly account, you can type:

npx @fastly/cli service list

Fastly CLI as a dependency package

Of course, availability on npmjs.org means you can now add the Fastly CLI to your Compute JavaScript application as a standard dependency:

npm install @fastly/cli

Alternatively, you can add it to your project’s package.json file:

{
  "dependencies": {
    "@fastly/cli": "^10.14.0"
  }
}

Then install the dependencies for the project:

npm install

This installs @fastly/cli as a dependency into your project’s node_modules directory. It becomes available as a program called fastly under the node_modules/.bin subdirectory, so you will be able to invoke it like this:

npx fastly

For example, to start your application in the local development environment, type the following:

npx fastly compute serve

Additionally, any references to fastly in the scripts section of the package.json file will now find this locally installed version of @fastly/cli instead of requiring a global installation of the Fastly CLI to be available on the system.

{
  "scripts": {
    "build": "js-compute-runtime src/index.js bin/main.wasm",
    "start": "fastly compute serve",
    "deploy": "fastly compute publish"
  }
}

No global installation necessary

Traditionally, it has been necessary for every developer working with Fastly Compute to install a globally available instance of the Fastly CLI to develop applications and publish them to their Fastly account, even when just getting started with the platform to experiment. By making the Fastly CLI available as a standard dependency to a JavaScript application, first-time users of Fastly Compute can experience a Compute application by simply cloning its application repository, installing its dependencies as normal, and typing npm start. In fact, we have updated all our JavaScript and TypeScript starter kits to take this approach, so that they can be experienced by more users, even if they have no prior experience with the Compute platform.

It’s also great when you work in a team. You, as well as other developers who work with your application, can obtain your application’s code, use the standard procedure to install its dependencies, and get right to work, batteries included.

This convenience extends to your continuous integration (CI) pipeline as well, enabling your application to have reliable access to the Fastly CLI as part of its build or test process.

Use new features of the CLI with confidence

The Fastly CLI is under active development, constantly receiving new features and improvements.

Specifying the Fastly CLI as a standard dependency of your package enables you to prescribe its version using semantic versioning. This allows your package to safely depend on features of the CLI that have been added recently or whose behavior may have changed, without having to worry about whether other developers who work with your application have a compatible version of the CLI installed on their environments.

Calling the Fastly CLI from Node.js code

If you are writing code intended to run under Node.js that calls out to the Fastly CLI, such as in a tool or utility program, this package provides a very straightforward and reliable way to do so.

The default export of @fastly/cli resolves to a string value representing the full path of the executable of the Fastly CLI, appropriate for the operating system, architecture, and how it has been installed. It can be used directly with functions such as spawnSync in Node.js. Because this is available in this way, you don’t need to have the user of your package obtain a global installation of the Fastly CLI before running your program.

The following example Node.js program executes the fastly version command:

import { spawnSync } from 'node:child_process';
import fastly from '@fastly/cli';

const result = spawnSync(fastly, [ 'version' ]);

console.log(result.stdout);

Even install the Fastly CLI globally

If you ever do need a global installation of the Fastly CLI, this package also acts as one way of obtaining it for global use:

npm install -g @fastly/cli

Once it’s installed, invoke it as you would have done traditionally:

fastly

Under the hood, this is no different than invoking the copy of the CLI cached in the environment using npx @fastly/cli. However, this procedure makes the command available as fastly on the system path, enabling it to fit with other tools that expect to find it there. The end result is effectively identical to if you had used any of the traditional methods of global installation, but this installation process is a convenient alternative, as npm is widely available on many machines.

Bringing edge computing to more developers

At Fastly, we strive to provide the tools that give you the power to run more code at the edge and develop for it with the tools you know and love. We want nothing to get in the way of this. As JavaScript is the most popular language on the planet, the release of the Fastly CLI on npm moves the needle further along this mission.

We love to hear about it when our users get the most out of these tools. Get your free Fastly developer account, join us on the Fastly community forum, and let us know what you’ve been building!