How to deploy Node.js application to Google Cloud (App Engine)?
Tags: node, cloud
Published: 2023-04-08 10:09:21

In this article, we will see how to deploy a Node.js application to [Google App Engine](https://cloud.google.com/appengine).

## Creating a Node.js application

Create a directory called `nodejs-gcp-deploy` and run the command `npm init -y` inside it.
This will create a default npm project.

Now install the following npm package inside the `nodejs-gcp-deploy` folder:

```
npm i express
```

Express helps in creating routes in Node.js applications.

Now create a file called `index.js` with the following code.

```js
const express = require("express")
const app = express()

app.get("/", (req, res) => {
  res.send("Hello World!")
})

app.get("/ping", (req, res) => {
  res.send({ success: true })
})

app.listen(process.env.PORT || 3000, () => {
  console.log("Server is listening on port 3000")
})
```

## Creating the app.yaml file

You need to specify what kind of project needs to be deployed using [app.yaml file](https://cloud.google.com/appengine/docs/standard/nodejs/runtime).

Create a file called `app.yaml` in the root directory of the project with the following code:

```yaml
runtime: nodejs16
```

## Creating a Project in Google Cloud

Navigate to the Google cloud console and create a new project:

![console dashboard dropdown](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwjpvzn8ofkjh8dne42e.png)

Now click on **NEW PROJECT**

![create new project 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iax1yndcpxgj75ed1tfx.png)

In the next screen provide a project name and click **CREATE**

![create new project 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rawn7pt4m4m0ddbvutv.png)

It will take a few minutes to create the project and you will receive a notification once finished.

![created notification](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzdeo8nrxju64tuhp019.png)

Now click on **SELECT PROJECT**. This will navigate you to the newly created project dashboard.

Now Search for **App Engine** and select it.

![create app engine 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oep126qbvn5rb2izvteu.png)

In the next screen click on **CREATE APPLICATION**

![create app engine 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5o9q24u14g09oztrigd.png)

Now choose the region where you would want your application to be hosted (always chose a region where your majority of users are from).

![choose region](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/600sxuprhch5ipb0fg87.png)

## Installing Google Cloud CLI

Install the google cloud CLI on your machine by running the following command in Powershell (Windows):

```shell
(New-Object Net.WebClient).DownloadFile("https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe", "$env:Temp\GoogleCloudSDKInstaller.exe")

& $env:Temp\GoogleCloudSDKInstaller.exe
```

If you are using any other operating system, you can find the instructions to install it [here](https://cloud.google.com/sdk/docs/install-sdk#installing_the_latest_version).

After the SDK is installed, open the command prompt in administration mode, navigate to the Node.js project directory,
and run the following command:

```
gcloud init
```

If you are running the above command for the first time, it will ask for logging in.
Once logged in choose the account where the project is created and then the project.

![gcloud init logs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4bkwd5kbges72mt8fqv1.png)

Now deploy the changes using the following command:

```
gcloud app deploy
```

![cloud build error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2bd3g5zyqg9w1c9rh6h.png)

If you face the above error, click on the link on the error and enable Cloud Build
API (you may have to enable billing).

![enable cloud build api](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/giklapq8qq832uq3etvb.png)

Re-run the command `gcloud app deploy` again and it should get deployed and print the URL of the application.