gCloud: File index.js does not exist in root directory

Viewed 5064

I am a first time user of Gcloud. When I run the following command:

gcloud beta functions deploy FirstBot --stage-bucket [BUCKET_NAME] --trigger-http

I'm getting this error in my cmd:

ERROR: (gcloud.beta.functions.deploy) OperationError: code=3, message=Function l
oad error: File index.js or function.js that is expected to define function does
n't exist in the root directory.

I have tried 2 index.js files: Here's no.1:

   /*
 HTTP Cloud Function.

 @param {Object} req Cloud Function request context.
 @param {Object} res Cloud Function response context.
*/
exports.FirstBot = function FirstBot (req, res) {
  response = "This is a sample response from your webhook!" //Default response from the webhook to show it's working

res.setHeader('Content-Type', 'application/json'); //Requires application/json MIME type
  res.send(JSON.stringify({ "speech": response, "displayText": response 
  //"speech" is the spoken version of the response, "displayText" is the visual version
  }));
};

Here's the second one:

/
 HTTP Cloud Function.

 @param {Object} req Cloud Function request context.
 @param {Object} res Cloud Function response context.
*/
exports.helloHttp = function helloHttp (req, res) {
  response = "This is a sample response from your webhook!" //Default response from the webhook to show it's working

res.setHeader('Content-Type', 'application/json'); //Requires application/json MIME type
  res.send(JSON.stringify({ "speech": response, "displayText": response 
  //"speech" is the spoken version of the response, "displayText" is the visual version
  }));
};

The name of my project is FirstBot. I have created a bucket also.

The path of my FirstBot folder is C:\FirstBot. The index.js file is inside it. I am following the tutorial at: https://api.ai/docs/getting-started/basic-fulfillment-conversation

Kindly help..Would be grateful!

7 Answers

The error can also occur if you're not running the deploy command from the same folder where the index.js is located. I.e. it's just "file not found".

I solved it by installing npm for JavaScript.

II was having a similar issue and solution for me was pretty easy to apply but hard to find. In case someone can help. I noticed that my .gcloudignore had this line

#!include:.gitignore

and my .gitignore these two

# Compiled JavaScript files    
**/*.js
**/*.js.map

Therefore .js files where being ignored and gcloud could not find index.js.

As i don't want compiled files to be in repository, i ended by removing the include at .gcloudignore and add the specific ignores directly, such as node_modules and sensitive data

Related