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

7

5

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!

Lysippus answered 26/7, 2017 at 4:30 Comment(2)
You shouldn't be entirely changing the context of the question as your investigation continues - invalidates everything... Instead ask a new question, eventually referencing the original question as context.Jurywoman
Were you able to resolve this issue? If so it is recommended to post your answer as the solution to better help the community. If you are still having issues it is recommended to run 'gcloud components update' to install the latest GCloud and follow the deployment guide.Oxen
L
1

I solved it by installing npm for JavaScript.

Lysippus answered 21/9, 2017 at 19:20 Comment(0)
S
5

Is the file in your .gitignore? If a .gcloudignore isn't specified, the .gitignore is used to ignore files.

Adding an empty .gcloudignore should fix this.

Spitzer answered 11/2, 2018 at 20:31 Comment(0)
P
4

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".

Punchball answered 12/10, 2018 at 10:46 Comment(0)
L
1

I solved it by installing npm for JavaScript.

Lysippus answered 21/9, 2017 at 19:20 Comment(0)
J
0

You're exceeding the Max deployment size limit. From Resource Limits:

enter image description here

Check the content of your project dir, you may have undesired stuff in there.

Jurywoman answered 26/7, 2017 at 4:45 Comment(4)
Hey Dan! My project dir path is C:\FirstBot and it has only 1 index.js fileLysippus
Try to bump the cmd's verbosity with --verbosity=debug (or info?), maybe logs will show where's that size coming from.Jurywoman
Hey, so I went into my FirstBot directory and ran the command : C:\FirstBot>gcloud beta functions deploy FirstBot --stage-bucket supple-cheme-648 --trigger-http AND IT IS GIVING ME THIS ERROR: Deploying function (may take a while - up to 2 minutes)...failed. 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.Lysippus
Can you post the content of the index.js file (or at least the FirstBot and the helloHttp functions)?Jurywoman
W
0

The command gcloud beta functions deploy FirstBot --stage-bucket [BUCKET_NAME] --trigger-http will deploy a function with name FirstBot by creating zip file with content of the directory from which your ran the command.

For the deployment to work you should:

  • Run the command when you're in C:\FirstBot, or
  • Add --source C:\FirstBot to your gcloud invocation to let gcloud know that your source code is in a different directory.
Whist answered 27/7, 2017 at 9:29 Comment(4)
Hey! I ran the command from C:\FirstBot from the start , but I'm getting the error as described in the question!Lysippus
Can you try running it with --local-path flag? If it doesn't help can you download the zip file gcloud uploads and see if it has content of C:\FirstBot?Whist
--local-path does not seem to be available in gcloud functions deploy or gcloud beta functions deployInquest
--local-path flag was replaced with --source since I wrote that answer. I'll edit the answer.Whist
C
0

Adding a bucket to the project via the console solved it for me.

Convey answered 18/12, 2017 at 19:16 Comment(0)
T
0

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

Tremain answered 5/3, 2021 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.