Home > Mobile >  Not able to start google cloud function on local server with terminal Visual Studio
Not able to start google cloud function on local server with terminal Visual Studio

Time:12-29

I am trying to develop my first Google Cloud Function but want to build and test locally. I have not done this before and I am just learning so please be patient with me.

First - I created my moralisaccount directory, created a package.json with npm init with default configs, and installed the functions-framework with npm install @google-cloud/functions-framework

AMAC02CLCBTLEE:moralisaccount youthdev$ nvm use node
Now using node v17.3.0 (npm v8.3.0)
AMAC02CLCBTLEE:moralisaccount youthdev$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (moralisaccount) 
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /Users/youthdev/MoralisAccount/package.json:

{
  "name": "moralisaccount",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes) 
AMAC02CLCBTLEE:moralisaccount youthdev$  npm install @google-cloud/functions-framework

added 103 packages, and audited 104 packages in 4s

8 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
AMAC02CLCBTLEE:moralisaccount youthdev$ 

Second - Created simple index.js file with a simple function in same moralisaccount directory as my package.json

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
 exports.moralisAccount = async (req, res) => {
    let message = req.query.message || req.body.message || 'Hello World!';
    res.status(200).send(message);
  };

Last - I ran the below in terminal within visual studio and I was not getting the expected response within terminal.

actual

Node.js v17.3.0
youthdev@AMAC02CLCBTLEE moralisaccount % node node_modules/@google-cloud/functions-framework --target=moralisAccount
youthdev@AMAC02CLCBTLEE moralisaccount % 

expected:

Node.js v17.3.0
youthdev@AMAC02CLCBTLEE moralisaccount % node node_modules/@google-cloud/functions-framework --target=moralisAccount


Serving function...
Function: moralisAccount
URL: http://localhost:8080/

Not sure at what step I messed up as I am a beginner. Thanks for any help!

CodePudding user response:

Tried to follow the steps using below link and it works fine.

https://github.com/GoogleCloudPlatform/functions-framework-nodejs

In your scenario, instead of running node node_modules/@google-cloud/functions-framework --target=moralisAccount try running npx @google-cloud/functions-framework --target=moralisAccount

Sample Output:

o@cloudshell:~/myapp/routes $ npx @google-cloud/functions-framework --target=helloWorld
Serving function...
Function: helloWorld
Signature type: http
URL: http://localhost:8080/

Make sure to run the command in your index.js directory.

  • Related