Skip to content

Commit

Permalink
docs(node): Add recipe for deploying serverless functions to Netlify (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ndcunningham committed Apr 4, 2023
1 parent 807884c commit 9e12953
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/generated/manifests/menus.json
Original file line number Diff line number Diff line change
Expand Up @@ -2589,6 +2589,14 @@
"children": [],
"disableCollapsible": false
},
{
"name": "Deploying Node.js serverless functions to Netlify",
"path": "/recipes/deployment/node-serverless-functions-netlify",
"id": "node-serverless-functions-netlify",
"isExternal": false,
"children": [],
"disableCollapsible": false
},
{
"name": "Serverless deployment with Deno Deploy",
"path": "/recipes/deployment/deno-deploy",
Expand Down Expand Up @@ -2616,6 +2624,14 @@
"children": [],
"disableCollapsible": false
},
{
"name": "Deploying Node.js serverless functions to Netlify",
"path": "/recipes/deployment/node-serverless-functions-netlify",
"id": "node-serverless-functions-netlify",
"isExternal": false,
"children": [],
"disableCollapsible": false
},
{
"name": "Serverless deployment with Deno Deploy",
"path": "/recipes/deployment/deno-deploy",
Expand Down
20 changes: 20 additions & 0 deletions docs/generated/manifests/recipes.json
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,16 @@
"path": "/recipes/deployment/node-server-fly-io",
"tags": ["deployment", "node"]
},
{
"id": "node-serverless-functions-netlify",
"name": "Deploying Node.js serverless functions to Netlify",
"description": "",
"file": "shared/recipes/deployment/node-serverless-functions-netlify",
"itemList": [],
"isExternal": false,
"path": "/recipes/deployment/node-serverless-functions-netlify",
"tags": ["deployment", "node"]
},
{
"id": "deno-deploy",
"name": "Serverless deployment with Deno Deploy",
Expand Down Expand Up @@ -1100,6 +1110,16 @@
"path": "/recipes/deployment/node-server-fly-io",
"tags": ["deployment", "node"]
},
"/recipes/deployment/node-serverless-functions-netlify": {
"id": "node-serverless-functions-netlify",
"name": "Deploying Node.js serverless functions to Netlify",
"description": "",
"file": "shared/recipes/deployment/node-serverless-functions-netlify",
"itemList": [],
"isExternal": false,
"path": "/recipes/deployment/node-serverless-functions-netlify",
"tags": ["deployment", "node"]
},
"/recipes/deployment/deno-deploy": {
"id": "deno-deploy",
"name": "Serverless deployment with Deno Deploy",
Expand Down
14 changes: 14 additions & 0 deletions docs/generated/manifests/tags.json
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,13 @@
"name": "Deploying a Node.js server to Fly.io",
"path": "/recipes/deployment/node-server-fly-io"
},
{
"description": "",
"file": "shared/recipes/deployment/node-serverless-functions-netlify",
"id": "node-serverless-functions-netlify",
"name": "Deploying Node.js serverless functions to Netlify",
"path": "/recipes/deployment/node-serverless-functions-netlify"
},
{
"description": "",
"file": "shared/recipes/deployment/deno-deploy",
Expand All @@ -858,6 +865,13 @@
"id": "node-server-fly-io",
"name": "Deploying a Node.js server to Fly.io",
"path": "/recipes/deployment/node-server-fly-io"
},
{
"description": "",
"file": "shared/recipes/deployment/node-serverless-functions-netlify",
"id": "node-serverless-functions-netlify",
"name": "Deploying Node.js serverless functions to Netlify",
"path": "/recipes/deployment/node-serverless-functions-netlify"
}
],
"deno": [
Expand Down
6 changes: 6 additions & 0 deletions docs/map.json
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,12 @@
"tags": ["deployment", "node"],
"file": "shared/recipes/deployment/node-server-fly-io"
},
{
"name": "Deploying Node.js serverless functions to Netlify",
"id": "node-serverless-functions-netlify",
"tags": ["deployment", "node"],
"file": "shared/recipes/deployment/node-serverless-functions-netlify"
},
{
"name": "Serverless deployment with Deno Deploy",
"id": "deno-deploy",
Expand Down
2 changes: 1 addition & 1 deletion docs/shared/recipes/deployment/node-server-fly-io.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ For existing projects, see the next section, otherwise you can skip to [deployme

### Configure existing projects

**Skip this step if you are not configuring an exist project.**
**Skip this step if you are not configuring an existing project.**

If you have an existing Node.js server project, you can add the same deployment capabilities as we've just covered. Firstly, if the project is not an Nx project you can initialize it as such by running the `npx nx init` command in your project. Next, we can add the `build` and `docker-build` targets by invoking a couple of generators.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Deploying Node.js serverless functions to Netlify

Deploying Node.js serverless functions to Netlify involes a few steps:

## Creating the project

For new workspaces you can create a Nx workspace with serverless function with one command:

```shell
npx create-nx-workspace@latest my-functions \
--preset=@nrwl/netlify \
--site=my-site \ # Site ID or name to deploy the functions
```

## Configuring existing projects

**Skip this step if you are not configuring an existing project.**

You will need to install `@nrwl/netlify` if you haven't already.

{% tabs %}
{% tab label="npm" %}

```shell
npm i -D @nrwl/netlify
```

{% /tab %}
{% tab label="yarn" %}

```shell
yarn add -D @nrwl/netlify
```

{% /tab %}
{% tab label="pnpm" %}

```shell
pnpm add -D @nrwl/netlify
```

{% /tab %}
{% /tabs %}

- Add serverless configuration by running the following command:

```shell
nx generate @nrwl/netlify:setup-serverless
```

- Create a new netlify serverless project with:

```shell
nx generate @nrwl/netlfiy:serverless
```

This will do a few things:

1. Create a new serverless function in `src/functions`.
2. Add the `netlify.toml` in the root of the project
3. Update your `project.json` to have 2 new targets `dev` & `deploy`.

## Configure your Netlify deploy settings

If you **do not** have a _site_ setup within your workspace, go inside the Netlify dashboard, and create/use an existing site where your serverless functions will be deployed.

In your `project.json` you can update your deploy site by going to the `deploy` target adding `--site=my-site-name-or-id` replace **my-site-name-or-id** to what you have in your Netlify dashboard.

It should look similar to:

```json
"deploy": {
"dependsOn": ["lint"],
"command": "npx netlify deploy --prod-if-unlocked --site=my-site"
}
```

## Deployment

To view your functions locally you run:

```shell
nx serve
```

Inside your `netlify.toml` your functions _path_ should be already configured.
To deploy them to Netlify you would run:

```shell
nx deploy
```

The netlify CLI will output the link where you functions can be accessed. You can either click that link or open your browser and navigate to it!

1 comment on commit 9e12953

@vercel
Copy link

@vercel vercel bot commented on 9e12953 Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev
nx-dev-nrwl.vercel.app

Please sign in to comment.