How to GCP Function locally with Node.js

2023/06/012 min read
bookmark this
Responsive image

Create Service Account

Create GCP service account and add corresponding role, create new key and save the file (JSON or P12) locally.

Load Service Account

Next, loading the file at the Node.js file

const options = {
    keyFileName: 'location_of_the_file.json',
    projectId: 'your_gcp_project_id'
};

const bigquery = new BigQuery(options);

const dataset = bigquery.dataset('my_dataset');

const table = dataset.table('my_table');

Run Node.js locally

Add below code to the package.json's script section, when run this at the command npm run test, I have test at MacOs with node version 18, anything below might not works.

 "test": "npx functions-framework --target=main [--signature-type=http]",

Test The Node.js with Big Query Access

Run the below code should be start http server locally, no you can verify, debug the code and make sure everything works before run the deploy.

npm run test

How to Debug via Visual Studio Code

Visual Studio Code is a great tools as software development, and has been improve a lot since beginning, once of the feature for debug Node.js at Visual Studio Code is, you can switch the JavaScript Debug Terminal and run the npm run test, this will use the build-in debugger attached to the process.

So pretty simple for debug Node.js with Visual Studio.