Firebase - Cloud Functions in CoffeeScript - firebase

I've read somewhere that you can write your Cloud Functions using CoffeeScript, but I'm not sure how.
I did
npm install --save-dev coffeescript
npm install --save-dev coffee-loader
in the functions directory and the devDependencies object does show the libs in package.json
"devDependencies": {
"coffee-loader": "^0.9.0",
"coffeescript": "^2.4.1",
"firebase-functions-test": "^0.1.6"
},
but doing
const backend = require('backend.coffee')
in index.js results in
Cannot find module 'backend.coffee'
Edit: The above error is solved by using a relative path (./backend.coffee), thanks #caffeinated.tech, but now the coffee file is being interpreted as simple JS code and fails.
The contents of package.json:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"dependencies": {
"firebase-admin": "^8.0.0",
"firebase-functions": "^3.1.0",
"nodemailer": "^6.3.1",
"qrcode": "^1.4.2"
},
"devDependencies": {
"coffee-loader": "^0.9.0",
"coffeescript": "^2.4.1",
"eslint": "^6.5.1",
"firebase-functions-test": "^0.1.6"
},
"private": true
}

Related

Firebase Cloud Functions - Error parsing triggers: Cannot find module 'core-js/fn/reflect'

While trying to redeploy a function I now get this error message: Error parsing triggers: Cannot find module 'core-js / fn / reflect'.
Here's my package.json:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "14"
},
"main": "index.js",
"dependencies": {
"#google-cloud/logging": "^9.1.0",
"#woocommerce/woocommerce-rest-api": "^1.0.1",
"algoliasearch": "^4.8.5",
"cors": "^2.8.5",
"csvtojson": "^2.0.10",
"express": "^4.17.1",
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.14.1",
"mkdirp": "^1.0.4",
"stripe": "^8.137.0"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0"
},
"private": true
}
In index.js i use
const cors = require('cors')({origin: true});
....
exports.StripeEvents = functions.https.onRequest((req, res) => {
cors(req, res, () => {
//// some code that worked great
}
}
I do not use cors-js package at all...
Where does the problem comes from?
Thank you
depending the browser that you are using and the Javascript that you are using in some cases they do not support all the features required by Firebase so you need to at the Polyfills, in this documentation they show you the way to add the Polyfills to your app, the no recommended way to do it is with the core-js library, feel free to do it what you want.
I quick way to solve the error could be go to the directory where your functions live and execute:
solution install core-js#2.6.5

Error: Error parsing triggers: Cannot find module 'firebase-functions'.?

How can I solve this below error?
Error: Error parsing triggers: Cannot find module 'firebase-functions'
Require stack:
- D:\budgram\functions\index.js
- C:\Users\91998\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js
Try running "npm install" in your functions directory before deploying.
package.json file here...
package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1",
"request-promise": "^4.2.6"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0"
},
"private": true
}

Firebase CLI deploy Error: There was an error reading functions/package.json

I recently getting this error when try to deploy the firebase cloud functions using the command firebase deploy --only functions. My package.json file is shown below
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "10"
},
"main": "lib/index.js",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"firebase-admin": "^8.12.1",
"firebase-functions": "^3.6.2",
"stripe": "^8.50.0"
},
"devDependencies": {
"#types/cors": "^2.8.6",
"dotenv": "^8.2.0",
"firebase-functions-test": "^0.2.0",
"tslint": "^5.12.0",
"typescript": "^3.8.0"
},
"private": true
}
Using npm package firebase-tools of version 8.4.1.
Also getting error in firebase emulators
Enter into functions directory
enter the following command npm run build
and then deploy / launch your emulators
To me what happened was that I added a test folder out side of src folder. Because I use typescript the typesctipt compiler lost it and changed the output directory.
So if that's the case, move the other folders inside src.

firebase deploy from outside functions folder

I have a workspace structured as such, where 'app' would normally be the 'functions' folder on firebase init
server
--- app
------ controllers
------ middleware
------ db.js
------ index.js //where I export the firebase endpoint
------ router.js
------ serve.js //the express app
--- dist
--- node_modules
--- firebase.json
--- package.json
I have no trouble running firebase serve from the root to serve the functions locally, but firebase deploy requires me to place both the package.json file and the ./dist folder in app.
Is there a way to deploy from the root, without placing the package.json and ./dist folder in app?
My package.json file:
{
"main": "./dist/index.js",
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"prepare": "rm -rf dist && babel ./app/ -d ./dist && mkdir dist/private && cp app/private/conf.json dist/private/conf.json",
"lint": "eslint app",
"serve": "firebase serve --only dist",
"shell": "firebase experimental:functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only dist",
"logs": "firebase functions:log"
},
"dependencies": {
"cors": "^2.8.4",
"cross-fetch": "^2.1.0",
"firebase-admin": "^5.12.1",
"firebase-functions": "^1.0.3",
"moment": "^2.22.1",
"uuid": "^3.2.1"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1",
"babel-runtime": "^6.26.0",
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0"
},
"private": true
}
My firebase.json file
{
"functions": {
"predeploy": [
"npm --prefix $RESOURCE_DIR run lint"
],
"source": "dist"
}
}

Different entry points in package.json for local environment vs cloud environment

I have written my firebase functions in ES2017 code, which I had to transpile since I cannot deploy functions with ES2017 JavaScript. The way I understand it is firebase serve serves up the functions to run in my local environment (which uses ES2017) and firebase deploy deploys the functions to the cloud (no ES2017).
Before I deploy, I npm run prepare and babel grabs my index.js file in the main folder, transpiles it and puts in in a /dist folder, along with some config files.
In order to get firebase to deploy from the ./dist folder, I set up the entry point "main": "./dist/index.js" in the package.json. However, this means when I want to firebase serve for my local environment, I serve up the transpiled functions from the ./dist folder. So I have to keep changing between "main": "index.js" and "main": "./dist/index.js" depending on whether I want to serve the functions locally or deploy the transpiled functions.
This is despite having specified where I want to serve and deploy from, like so:
"scripts": {
"lint": "./node_modules/.bin/eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase experimental:functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions/dist",
"logs": "firebase functions:log"
}
My entire package.json file:
{
"main": "./dist/index.js",
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "./node_modules/.bin/eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase experimental:functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions/dist",
"logs": "firebase functions:log"
},
"dependencies": {
"babel-runtime": "^6.26.0",
"cors": "^2.8.4",
"cross-fetch": "^2.1.0",
"firebase-admin": "^5.8.2",
"firebase-functions": "^1.0.1",
"moment": "^2.22.1"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1",
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0"
},
"scripts": {
"prepare": "babel ./*.js --retain-lines -d ./dist && cp -a ./private ./dist/private",
"lint": "./node_modules/.bin/eslint --max-warnings=0 ."
},
"private": true
}

Resources