googlearchive / cloud-functions-emulator

A local emulator for deploying, running, and debugging Google Cloud Functions.
https://github.com/GoogleCloudPlatform/cloud-functions-emulator/wiki
Apache License 2.0
826 stars 114 forks source link

failed to deploy function #74

Closed niztal closed 7 years ago

niztal commented 7 years ago

After running functions deploy myFun --trigger-http, I get this error:

Error: functions.config() is not available. Please use the latest version of the Firebase CLI to deploy this function. at init (C:\Projects\tismo_cloud_functions\functions\node_modules\firebase-functions\lib\config.js:46:19) at Object.config (C:\Projects\tismo_cloud_functions\functions\node_modules\firebase-functions\lib\config.js:29:9) at Object. (C:\Projects\tismo_cloud_functions\functions\index.js:5:31) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.require (module.js:497:17) at require (internal/module.js:20:19)

OS: Windows 10 node version: 6.10.0 npm version: 3.10.3 firebase-functions: 0.5.2 firebase-tools: 3.5.0

jamesdaniels commented 7 years ago

We're still working on first class support for Cloud Functions for Firebase. In the meantime you'll need to manually create a config.json in your functions directory for the emulator to work.

niztal commented 7 years ago

I see..

can you please explain how to create config.json and what values should I set?

Thanks.

jamesdaniels commented 7 years ago

Depends what you're using. If it's the admin sdk, you'll need to put in your application default credentials. If you've set any config() values you should put those in too. firebase functions:config:get will show you what you've set or just console.log(functions.config()); from your productions version to get an idea what the json looks like.

niztal commented 7 years ago

Can someone please help me with that? I can't manage to set up config file. The documentation is not clear how to do that, so I won't get this "Error: functions.config() is not available. Please use the latest version of the Firebase CLI to deploy this function."

gerhardcit commented 7 years ago

I have a similar issue. The structure of the functions folder seems to have changed with beta release https://firebase.google.com/docs/functions/get-started

index.js is now in the functions/index.js

so when running this functions deploy helloWorld --trigger-http

The error: Error: Cannot find module /Users/xxx/project1 relates to the index.js file being looked for in the root, while it is now here /Users/xxx/project1/functions/index.js

gerhardcit commented 7 years ago

Having said that you can do this:

functions deploy helloWorld --trigger-http --local-path=./functions
gerhardcit commented 7 years ago

@niztal , the format of the config.json in the functions folder is this:

{
    "firebase": {
        "databaseURL": "https://<YOUR-PROJECT-ID>.firebaseio.com",
        "credential": {
            "type": "service_account",
            "project_id": "<your-project-id>",
            "private_key_id": "<theKey>",
            "private_key": "-----BEGIN PRIVATE KEY-----bla bla bla bla =\n-----END PRIVATE KEY-----\n",
            "client_email": "somethinghere@<your-project-id>.iam.gserviceaccount.com",
            "client_id": "<some numbers>",
            "auth_uri": "https://accounts.google.com/o/oauth2/auth",
            "token_uri": "https://accounts.google.com/o/oauth2/token",
            "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
            "client_x509_cert_url": "<and all that>"
        }
    }
}

following this example https://firebase.google.com/docs/functions/write-firebase-functions

You can then write to your firebase db from a local deploy. this is the full index.js file

var functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);  // this looks for config.json in the functions folder

exports.helloWorld = functions.https.onRequest((req, res) => {
  // Grab the text parameter.
  const original = req.query.text || 'Nothing said';
  // Push it into the Realtime Database then send a response
  admin.database().ref('/messages').push({original: original}).then(snapshot => {
    // Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
    //res.redirect(303, snapshot.ref); // don't do this when curling
    res.send(snapshot.key);
  });
});

deploy it

functions deploy helloWorld --trigger-http --local-path=./functions

and test it

curl 'http://localhost:8010/<your-project-id>/us-central1/helloWorld?text=xxxx'

Remember to have functions running before you do this functions start

niztal commented 7 years ago

Great I'll test it.. thank a lot

varunarora commented 7 years ago

Hey @gerhardcit thanks for those tips!

I get the following error message:

FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error: \"getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80\"."}

Do you possibly know what this might be about?

gerhardcit commented 7 years ago

@varunarora , have you used the credentials somewhere else successfully?. Only seems that your credentials and the url might not be aligned?

justinhandley commented 7 years ago

Where do you get the information listed under credential?

qalqi commented 7 years ago

@gerhardcit Followed your steps and I am getting error message Failed to get token: Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80".

Tried the following trouble shoots 1) firebase login and firebase logout 2) Change local time to US time 3) Create new projectname-firebase-adminsdk-xxxxx.json and updated config.json 4) Tested in multiple internet networks.

What could be issue? Thanks in Advance

jmdobry commented 7 years ago

Note, you no longer need to use the Cloud Functions Emulator directly as the emulator has been integrated into the Firebase SDK. See Running Functions Locally in the Firebase docs. Using the Firebase SDK to run your functions locally should eliminate these .config() issues.

valorloff commented 7 years ago

Hi! this is latest firebase SDK:

sudo firebase serve --only hosting,functions FirebaseError: Error occurred while parsing your function triggers. Error: functions.config() is not available. Please use the latest version of the Firebase CLI to deploy this function. at init (/home/orloff/functions-samples/isomorphic-react-app/functions/node_modules/firebase-functions/lib/config.js:46:19) at Object.config (/home/orloff/functions-samples/isomorphic-react-app/functions/node_modules/firebase-functions/lib/config.js:29:9) at Object. (/home/iam/functions-samples/isomorphic-react-app/functions/index.js:28:29) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.require (module.js:497:17) at require (internal/module.js:20:19)