Revenir au blog

Follow and Subscribe

Disponible uniquement en anglais

Cette page n'est actuellement disponible qu'en anglais. Nous nous excusons pour la gêne occasionnée, merci de revenir sur cette page ultérieurement.

Start your next Fastly Compute JavaScript application with npm init

Katsuyuki Omuro

Senior Software Engineer, Developer Relations, Fastly

The npm init (or npm create) command is a popular, platform-standard method of initializing a JavaScript application based on a template. If you’re working with JavaScript in Fastly Compute, we have exciting news for you – the npm init script for Fastly Compute gives JavaScript authors an additional way to start their Fastly Compute applications using common and standard tools.

Just over a year ago, JavaScript became a generally available language for development on Fastly Compute. With this, we have been able to bring the convenience of JavaScript to the power of the edge to developers around the world. JavaScript is a very popular language on the platform, and we want to continue to give it access to as many users as we can.

In October we announced that we have brought the Fastly CLI to npm. Today we have additional good news for JavaScript users: we now have an official npm init script for Fastly Compute!

In case you’re not familiar with npm init, it is a method of initializing a new JavaScript project based on an initialization script, built into the npm command. Fastly now provides an initializer for npm init, so it’s possible to create a new Fastly Compute project in JavaScript like this:

npm init @fastly/compute

After typing this command, you will be presented with interactive prompts to select a directory, language, and starter kit, to create your Fastly Compute application in JavaScript or TypeScript.

The initializer called by npm init @fastly/compute is a Node.js program distributed via npm under the name @fastly/create-compute. It asks questions relevant to your new JavaScript application, and then under the hood, calls the Fastly CLI to initialize your new application. No global installation of the CLI is required, however, since it declares the dependency as an npm package.

You’re already working with JavaScript, stay in the JavaScript mindset

Traditionally, the procedure to create a new Fastly Compute application has been to obtain the Fastly CLI and type the following:

fastly compute init

This meant that it was necessary for you to install a global instance of the Fastly CLI just to create a new application for Fastly Compute. You also needed it to run and publish the application. Because this was true even if you just wanted to experiment with the platform, we felt that this was sometimes getting in the way of developers wanting to try out the platform. Additionally, the Fastly CLI would prompt the selection of a programming language when initializing a new project, a potential friction point that could even take you “out of the zone” when you are already thinking in terms of JavaScript.

As always, we looked for ways to enable developers to get the job done with one fewer tool needed to be installed, with one fewer click, with one fewer dependency. Is there a way to enable JavaScript development to be even simpler? The solution was to use the standard, platform-defined npm init mechanism to allow users to initialize new applications for Fastly Compute in JavaScript. Most JavaScript developers are already familiar with npm, since it's the tool used to install dependency packages into your application. Being able to use the npm tool to initialize a new application enables developers to stay with tools within the JavaScript ecosystem from the start of the lifetime of their application.

Batteries included!

Recently, Fastly’s JavaScript and TypeScript starter kits have been updated to install the Fastly CLI as a dependency package. This means that for most tasks you no longer need to work with the Fastly CLI directly, to run or publish your application. Instead, you are able to work with it through npm scripts.

Once your application has been initialized, use the following command to start your application:

npm run start

This will run fastly compute serve, which will build your application and start it under the development environment.

When you’re ready to go live, type the following command to deploy to production (for most starter kits):

npm run deploy

This will run fastly compute publish, which will build your application and publish it to your Fastly account.

Of course, if you would like to call the Fastly CLI directly, or in the case you need to perform additional functions with the Fastly CLI, such as add backends or work with edge storage, you can invoke it from within your application directory using npx:

npx fastly <command>

JavaScript + edge computing = ❤️

At Fastly, we enjoy providing you with the power to run more code at the edge and develop for it with the tools you know and love. One fewer thing to get in the way is one more victory for us in that direction. Our initializer enabling you to create JavaScript applications for Fastly Compute using npm is another item on this journey.

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!