I'm trying to build a CI/CD pipeline for my project that uses Firebase Cloud Functions.
The functions code is written in TypeScript.
Deploying the code from my local MacBook works perfectly. However, when deploying the same exact code from the GitHub Action runner it fails with an error:
/home/runner/work/geonotes-backend/geonotes-backend/functions/lib/handlers/createNoteActionHandler.js:11
return sendOutput({ note_id: note?.id });
^
SyntaxError: Unexpected token '.'
at wrapSafe (internal/modules/cjs/loader.js:1052:16)
at Module._compile (internal/modules/cjs/loader.js:1100:27)
at Module._compile (pkg/prelude/bootstrap.js:1394:32)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1156:10)
at Module.load (internal/modules/cjs/loader.js:984:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Module.require (internal/modules/cjs/loader.js:1024:19)
at Module.require (pkg/prelude/bootstrap.js:1338:31)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/home/runner/work/geonotes-backend/geonotes-backend/functions/lib/index.js:28:51)
This looks like Firebase is not accepting the optional chaining syntax, but it should since the function runs on Node.js 14.
Furthermore, the same syntax is accepted perfectly by the CLI running on my machine.
I have verified that both my machine and the GitHub runner are using the same versions:
Node.js v14.17.4
NPM v6.14.14
Firebase CLI v9.16.0
TypeScript v4.3.5
I have also tried to run my action on macos-latest instead of ubuntu-latest with no luck.
This is one of the runs that is failing, with --debug enabled:
https://github.com/emilioschepis/geonotes-backend/runs/3261106553
Does anyone have an idea about what could be wrong?
Related
as you all aware about firebase CLI's new feature which is local emulators explained here(https://firebase.googleblog.com/2020/05/local-firebase-emulator-ui.html?m=1)
so I have updated my CLI and when I run firebase emulators:start I get this error in my functions code
kishan#kishans-Air functions % firebase emulators:start
i emulators: Starting emulators: functions
⚠ Your requested "node" version "8" doesn't match your global version "10"
⚠ hosting: The hosting emulator is configured but there is no hosting configuration. Have you run firebase init hosting?
i ui: Emulator UI logging to ui-debug.log
i functions: Watching "/Users/kishan/Desktop/dilip/googlecloudfunctions/functions" for Cloud Functions...
⚠ TypeError: Cannot convert object to primitive value
at Proxy.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:311:83)
at Object.<anonymous> (/Users/kishan/Desktop/dilip/googlecloudfunctions/functions/lib/index.js:7:7)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:661:33
⚠ We were unable to load your functions code. (see above)
- It appears your code is written in Typescript, which must be compiled before emulation.
can anyone help what I need to do here ??
The error message is suggesting to you:
It appears your code is written in Typescript, which must be compiled before emulation.
You have to build the code first with npm run build. The emulator will not do that for you.
I found that instead of calling:
admin.initializeApp(functions.config().firebase)
I had to instead call:
admin.initializeApp({
credential: admin.credential.applicationDefault()
});
This resolved the issue.
I am trying to test my cloud functions locally. I have a custom functions configuration variable, so I used the command firebase functions:config:get > .runtimeconfig.json to generate a runtimeconfig.json file:
{
"stripe": {
"testkey": "someKey"
}
}
Then I use the command firebase functions:shell to start the emulator, but it prints the following error in my terminal:
+ functions: Emulator started at http://localhost:5001
! TypeError: Cannot read property 'testkey' of undefined
at Object.<anonymous> (C:\Users\Jesper\intergun\functions\lib\index.js:14:52)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at C:\Users\Jesper\AppData\Roaming\nvm\v8.16.0\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:532:33
at Generator.next (<anonymous>)
! We were unable to load your functions code. (see above)
- It appears your code is written in Typescript, which must be compiled before emulation.
- You may be able to run "npm run build" in your functions directory to resolve this.
For some reason it cannot find the variable, and it also seems like it thinks the code is written in TypeScript, even though it is pointing to the generated index.js file. I tried running npm run build but the result is the same.
The top of my index.ts looks like this:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import 'firebase-functions';
import * as Stripe from 'stripe';
admin.initializeApp();
const stripe = new Stripe(functions.config().stripe.testkey);
What am I missing?
I found the solution here: https://github.com/firebase/firebase-tools/issues/711
To generate .runtimeconfig.json, I used the command firebase functions:config:get > .runtimeconfig.json. However, this generated a file which seemed to be correct but apparently had invisible characters that shouldn't be there. Instead, I used this command: firebase functions:config:get | ac .runtimeconfig.json. This solved the problem.
I'm attempting to build an executable of a small NextJS example app based on the example here using zeit/pkg. Problem is the only feasible entry point (server.js) seems to give me a bunch of errors not currently listed in their issues related to webpack compilation.
If you'd like to reproduce just follow the instructions here and then add pkg. Also - here's the error output below.
ERROR Failed to compile with 2 errors
10:48:35 AM These dependencies were not found:
#babel/runtime/helpers/extends in D:/snapshot/pkg-ssr/node_modules/next/dist/lib/app.js
webpack-hot-middleware/client?autoConnect=false&overlay=false&reload=true
in
D:/snapshot/pkg-ssr/node_modules/next/dist/client/webpack-hot-middleware-client.js
To install them, you can run: npm install --save
#babel/runtime/helpers/extends
webpack-hot-middleware/client?autoConnect=false&overlay=false&reload=true
(node:15964) DeprecationWarning: Module.chunks: Use
Module.forEachChunk/mapChunks/getNumberOfChunks/isInChunk/addChunk/removeChunk
instead
Ready on http://localhost:3000 Client pings, but there's no entry for page: /about { Error: Cannot find module
'D:_experiments\pkg-ssr.next\build-manifest.json'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:592:15)
at Function.Module._resolveFilename (pkg/prelude/bootstrap.js:1278:46)
at Function.Module._load (internal/modules/cjs/loader.js:518:25)
at Module.require (internal/modules/cjs/loader.js:648:17)
at Module.require (pkg/prelude/bootstrap.js:1157:31)
at require (internal/modules/cjs/helpers.js:20:18)
at _callee3$ (D:\snapshot\pkg-ssr\node_modules\next\dist\server\render.js:202:29)
at tryCatch (D:\snapshot\pkg-ssr\node_modules\regenerator-runtime\runtime.js:62:40)
at Generator.invoke [as _invoke] (D:\snapshot\pkg-ssr\node_modules\regenerator-runtime\runtime.js:296:22)
at Generator.forEach.prototype.(anonymous function) [as next] (D:\snapshot\pkg-ssr\node_modules\regenerator-runtime\runtime.js:114:21)
at step (D:\snapshot\pkg-ssr\node_modules\#babel\runtime\helpers\asyncToGenerator.js:12:30)
at _next (D:\snapshot\pkg-ssr\node_modules\#babel\runtime\helpers\asyncToGenerator.js:27:9)
at process._tickCallback (internal/process/next_tick.js:68:7) code: 'MODULE_NOT_FOUND' } Client pings, but there's no entry for
page: /about { Error: Cannot find module
'D:_experiments\pkg-ssr.next\build-manifest.json'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:592:15)
at Function.Module._resolveFilename (pkg/prelude/bootstrap.js:1278:46)
at Function.Module._load (internal/modules/cjs/loader.js:518:25)
at Module.require (internal/modules/cjs/loader.js:648:17)
at Module.require (pkg/prelude/bootstrap.js:1157:31)
at require (internal/modules/cjs/helpers.js:20:18)
at _callee3$ (D:\snapshot\pkg-ssr\node_modules\next\dist\server\render.js:202:29)
at tryCatch (D:\snapshot\pkg-ssr\node_modules\regenerator-runtime\runtime.js:62:40)
at Generator.invoke [as _invoke] (D:\snapshot\pkg-ssr\node_modules\regenerator-runtime\runtime.js:296:22)
at Generator.forEach.prototype.(anonymous function) [as next] (D:\snapshot\pkg-ssr\node_modules\regenerator-runtime\runtime.js:114:21)
at step (D:\snapshot\pkg-ssr\node_modules\#babel\runtime\helpers\asyncToGenerator.js:12:30)
at _next (D:\snapshot\pkg-ssr\node_modules\#babel\runtime\helpers\asyncToGenerator.js:27:9)
at process._tickCallback (internal/process/next_tick.js:68:7) code: 'MODULE_NOT_FOUND' }
In my case, the issue was that I wasn't packaging a built app. I needed to do a few things:
specify the assets and scripts in the package.json like below (note that dist was actually server in my case because I had a custom server and middleware)
"pkg": {
"assets": [
".next/**/*"
],
"scripts": [
".next/dist/**/*.js"
]
},
build the app first using next build
then ensure that whichever way I built the package, I used the package.json. For me the best command was their default example: pkg .
See the following article from Mike Hsu in case you want a repo and instructions to compare to: https://medium.com/#evenchange4/deploy-a-commercial-next-js-application-with-pkg-and-docker-5c73d4af2ee
I'm testing firebase functions locally in my existing project in typescript. When i run following command, i always get typeerror even though i can upload it to firebase project. the error always occured after typescript compile to js
firebase serve --only functions
TypeError: Cannot read property 'username' of undefined
at Object. (C:\Users\phone\Desktop\VMS\mynewvm_functions\functions\lib\email\index.js:8:52)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object. (C:\Users\phone\Desktop\VMS\mynewvm_functions\functions\lib\index.js:19:15)
at Module._compile (module.js:635:30)
import * as functions from 'firebase-functions';
import * as nodemailer from 'nodemailer';
const accountname = functions.config().emailaccount.username;
const accountpassword = functions.config().emailaccount.password;
/* smtp configuration */
I already set emailaccount in my project, below is my code.
firebase functions:config:set emailaccount.username="email#domain.com"
emailaccount.password="mypassword"
Please note that it is working fine when uploaded to firebase functions but it's not when serving locally. what can i do to make it work locally?
Finally, I just replace all my credentials with actual values instead of getting from firebase config. And I cd to functions folder and type 'npm start'. it look like when i run 'firebase serve --only functions', it doesn't apply with recent changes so give me the same error until I type 'npm start' which will run tsc and firebase shell, stop it (ctrl+c) then type 'firebase serve --only functions' again. Fyi, I created my current firebase functions project using firebase cli and typescript.
firebase cli - 3.18.4
firebase functions - ^1.0.1
firebase admin - 5.12.0
#google-cloud/storage - 1.6
After testing for long period of time, I decided to put username, password and tokens directly in my functions for local testing although I don't really like this idea and I change back to functions.config().. when i deploy to firebase. Thanks for #Frank for answers
There is no built-in config parameter called emailaccount, so it seems you defined it yourself.
The local environment does not automatically contain these variables. You will actually have to define them in a file called runtimeconfig.json. An easy way to do that is shown here:
cd functions
firebase functions:config:get > .runtimeconfig.json
I have a Firebase project that I've created earlier this year. It uses Cloud Functions to run some operations on the Realtime Database.
Yesterday, I learned about the Callable Cloud Functions, so I decided to try it in my app to see if I should update my existing Functions or not. I've created a simple Cloud Function for testing purposes:
exports.testCall = functions.https.onCall((data, context) =>{
console.log(context.auth.uid);
return {response:"This means success"};
});
But when I try to deploy this function, I get the error:
Error: Error occurred while parsing your function triggers.
TypeError: functions.https.onCall is not a function
at Object. (/tmp/fbfn_7614ijhDwX0NY98S/index.js:114:36)
at Module._compile (module.js:649:30)
at Object.Module._extensions..js (module.js:660:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:501:12)
at Function.Module._load (module.js:493:3)
at Module.require (module.js:593:17)
at require (internal/module.js:11:18)
at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:18:11
at Object. (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:32:3)
I'm aware that Callable Cloud Functions has been introduced in the latest Firebase SDK, so I've tried updating it using:
sudo npm install -g firebase-tools
But I can't yet deploy my Cloud Function. I've also tried a partial deploy, as shown in the Firebase docs, but it didn't work. Is there something I'm missing in the documentation?
Simply updating your Firebase CLI won't solve the problem because you must also update the Cloud Functions SDK in the Project Directory. You mentioned the Firebase Project has been initialized earlier this year, so that's before the release of Callable Cloud Functions.
See, when you init Cloud Functions in your Firebase Project, it creates the functions directory which contains the Cloud Functions SDK. So you (probably) still have the old SDK in the functions directory and you need to update it. To do that, navigate to that directory and run the command:
sudo npm i --save firebase-functions#latest