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"
}
}
Related
I am following this tutorial about using google cloud tasks to trigger a cloud function. However I am unable to upload the code because i get thrown this error. "Require statement not part of import statement" and it references this code:
const {CloudTasksClient} = require("#google-cloud/tasks");
Ive tried multiple different resolves such as this one and this one. I am following the code and instructions exactly with no success. Does anybody have any suggestions for fixing this or another way of importing google cloud tasks? Any help would be great!
Edit:
I have triedimport {CloudTasksClient} from "#google-cloud/tasks" and it throws back errors at my variables such as
(Type 'string' is not assignable to type '"POST" | HttpMethod | "HTTP_METHOD_UNSPECIFIED" | "GET" | "HEAD" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | null | undefined'.)
here is my packages.json file:
{
"name": "functions",
"scripts": {
"lint": "eslint --ext .js,.ts .",
"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": "12"
},
"main": "lib/index.js",
"dependencies": {
"#google-cloud/tasks": "^2.3.6",
"firebase-admin": "^9.6.0",
"firebase-functions": "^3.15.3"
},
"devDependencies": {
"#typescript-eslint/eslint-plugin": "^3.9.1",
"#typescript-eslint/parser": "^3.8.0",
"eslint": "^7.6.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-import": "^2.22.0",
"firebase-functions-test": "^0.2.0",
"typescript": "^3.8.0"
},
"private": true
}
look into this documentation and change accordingly
https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-var-requires.md
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.
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
}
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
}
In a basic Firebase Functions project, the package.json file is being created in the functions folder. Now we're going to use CircleCI for our project. To getting this CI to work, the package.json needs to be in the top folder of your repo and not in any other subfolder as seen in this post here: https://discuss.circleci.com/t/cant-run-npm-install/19012
Now if I move the file to the root and correcting all the paths, I'm able to build and to use lint on the project, but the deploy is failing with the error, that firebase has got no directory path of the project. It looks like there is a hard coded method in firebase-tools which is preventing the moving of the package.json outside of the functions folder.
If I duplicate the package.json and put it back into the functions folder, everything is working fine but this is not a solution.
Here is my desired structure:
myproject
|
+- .firebaserc
+- firebase.json
+- package.json
+- tsconfig.json
+- tslint.json
|
+- functions/
|
+- src/
| |
| +- index.ts
|
+- lib/
|
+- index.js
+- index.js.map
package.json
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"main": "functions/lib/index.js",
"dependencies": {
"firebase-admin": "~5.12.0",
"firebase-functions": "^1.0.1"
},
"devDependencies": {
"tslint": "^5.8.0",
"typescript": "^2.5.3"
},
"private": true
}
tsconfig.json
{
"compilerOptions": {
"lib": ["es6"],
"module": "commonjs",
"noImplicitReturns": true,
"outDir": "./functions/lib",
"sourceMap": true,
"target": "es6"
},
"compileOnSave": true,
"include": [
"./functions/src"
]
}
Does anybody has already tried Firebase Functions with CircleCI or has any clue how to get this to work?
Okay, I got this thing up and running. You need to specify the working directory for every command in CircleCI to be exactly ~/project/functions. Now CircleCI is finding the package.json file.
This is our config.yml for CircleCI:
version: 2
jobs:
build:
docker:
# specify the version you desire here
- image: circleci/node:6.14
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "./functions/package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
# Install all needed dependencies from package.json
- run:
working_directory: ~/project/functions
command: yarn install
# Save the cache including node_modules folder
- save_cache:
paths:
- ~/project/functions/node_modules
key: v1-dependencies-{{ checksum "./functions/package.json" }}
# Create the folder for your unit test results
- run:
working_directory: ~/project/functions
command: mkdir junit
# run tests with mocha!
- run:
working_directory: ~/project/functions
command: yarn test_ci
environment:
MOCHA_FILE: junit/test-results.xml
when: always
- store_test_results:
path: ~/project/functions/junit
- store_artifacts:
path: ~/project/functions/junit
And the package.json with extra command test_ci for unit testing on CircleCI:
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log",
"test": "mocha --require ts-node/register ./src/test/*.spec.ts",
"test_ci": "mocha --require ts-node/register --reporter mocha-junit-reporter ./src/test/*.spec.ts"
},
"main": "lib/index.js",
"dependencies": {
"#google-cloud/firestore": "^0.13.1",
"#google-cloud/storage": "^1.6.0",
"#google-cloud/vision": "^0.17.0",
"firebase-admin": "^5.12.0",
"firebase-functions": "^1.0.1"
},
"devDependencies": {
"#types/chai": "^4.1.3",
"#types/mocha": "^5.2.0",
"#types/sinon": "^4.3.1",
"chai": "^4.1.2",
"firebase-functions-test": "^0.1.1",
"firebase-tools": "^3.18.0",
"mocha": "^5.1.1",
"mocha-junit-reporter": "^1.17.0",
"sinon": "^4.5.0",
"ts-node": "^5.0.1",
"tslint": "^5.8.0",
"typescript": "^2.5.3"
},
"private": true
}