Serverless Deployment with Deno Deploy

In this guide, we'll show you how to deploy serverless functions using Deno Deploy.

Prerequisite: A Deno Project

Have Deno already installed?

Make sure you have Deno installed on your machine. Consult the Deno docs for more details

If you don't have a Nx Deno project yet, you can easily create a new one with the following command:

npx create-nx-workspace@latest denoapp --preset=@nx/deno

Nx 15 and lower use @nrwl/ instead of @nx/

This creates a single Deno application.

You can also add a new Deno application to an existing Nx monorepo workspace. Make sure you have the @nx/deno package installed:

nx add @nx/deno

Directory Flag Behavior Changes

The command below uses the as-provided directory flag behavior, which is the default in Nx 16.8.0. If you're on an earlier version of Nx or using the derived option, omit the --directory flag. See the as-provided vs. derived documentation for more details.

Then generate a new Deno app with the following command:

nx g @nx/deno:app denoapp --directory=apps/denoapp

Nx 15 and lower use @nrwl/ instead of @nx/

Configuring Deno Deploy

First configure your Deno Deploy project:

  1. Push your repository to GitHub.
  2. Go to Deno dashboard and set up your Deno project. You need to authorize GitHub to allow access to your repositories, then you need to specify the main file (e.g. src/main.ts), and the production branch (e.g. main).
  3. Generate an access token from your account settings page. Copy the new token somewhere.
  4. Add an entry to the project's .env file: DENO_DEPLOY_TOKEN=<token-from-previous-step> (create this file if needed, and add it to .gitignore so you don't commit it)
  5. Install the deployctl CLI tool.

deployctl is a CLI that allows us to deploy our Deno project. We can embed that into our Nx project by creating a run-command. The @nx/deno plugin already comes with a setup-deploy generator that helps with that. Just run:

nx g @nx/deno:setup-deploy --platform=deno-deploy

Nx 15 and lower use @nrwl/ instead of @nx/

This adds a new target to your project.json

1{ 2 "name": "denoapp", 3 ... 4 "targets": { 5 ... 6 "deploy": { 7 "executor": "nx:run-commands", 8 "options": { 9 "command": "deployctl deploy --project=<Your-Deno-Deploy-Project-Name> --import-map=import_map.json --exclude=node_modules src/main.ts --dry-run" 10 }, 11 "configurations": { 12 "preview": { 13 "command": "deployctl deploy --project=<Your-Deno-Deploy-Project-Name> --import-map=import_map.json --exclude=node_modules src/main.ts" 14 }, 15 "production": { 16 "command": "deployctl deploy --project=<Your-Deno-Deploy-Project-Name> --import-map=import_map.json --exclude=node_modules --prod src/main.ts" 17 } 18 } 19 } 20 } 21} 22 23

Deploy

Once you are done the above steps, you can deploy and view your Deno app using the following command:

nx deploy

You can find the production URL from the Deno dashboard (e.g. https://acme-denoapp.deno.dev/). Browsing to the production URL should return the default JSON message: { "message": "Hello denoapp" }.