search
HomeWeb Front-endJS TutorialFastly CLI on npm: now at your JavaScript fingertips

Fastly CLI on npm: now at your JavaScript fingertips

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:

npx @fastly/cli 

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:

npx @fastly/cli service list

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

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

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!

The above is the detailed content of Fastly CLI on npm: now at your JavaScript fingertips. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Demystifying JavaScript: What It Does and Why It MattersDemystifying JavaScript: What It Does and Why It MattersApr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Is Python or JavaScript better?Is Python or JavaScript better?Apr 06, 2025 am 12:14 AM

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

How do I install JavaScript?How do I install JavaScript?Apr 05, 2025 am 12:16 AM

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.

How to send notifications before a task starts in Quartz?How to send notifications before a task starts in Quartz?Apr 04, 2025 pm 09:24 PM

How to send task notifications in Quartz In advance When using the Quartz timer to schedule a task, the execution time of the task is set by the cron expression. Now...

In JavaScript, how to get parameters of a function on a prototype chain in a constructor?In JavaScript, how to get parameters of a function on a prototype chain in a constructor?Apr 04, 2025 pm 09:21 PM

How to obtain the parameters of functions on prototype chains in JavaScript In JavaScript programming, understanding and manipulating function parameters on prototype chains is a common and important task...

What is the reason for the failure of Vue.js dynamic style displacement in the WeChat mini program webview?What is the reason for the failure of Vue.js dynamic style displacement in the WeChat mini program webview?Apr 04, 2025 pm 09:18 PM

Analysis of the reason why the dynamic style displacement failure of using Vue.js in the WeChat applet web-view is using Vue.js...

How to implement concurrent GET requests for multiple links in Tampermonkey and determine the return results in sequence?How to implement concurrent GET requests for multiple links in Tampermonkey and determine the return results in sequence?Apr 04, 2025 pm 09:15 PM

How to make concurrent GET requests for multiple links and judge in sequence to return results? In Tampermonkey scripts, we often need to use multiple chains...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor