firebase cloud function deploy glob error - javascript

When deploying my cloud functions using the command firebase deploy I am getting this error for all of my functions:
! functions[deleteGame-DeleteGameFunction(us-central1)]: Deployment error.
Function load error: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'glob'
at Function.Module._resolveFilename (module.js:476:15)
at Function.Module._load (module.js:424:25)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/user_code/index.js:11:14)
at Module._compile (module.js:577:32)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
This is my index.js file (I got it from here):
'use strict';
/** EXPORT ALL FUNCTIONS
*
* Loads all `.f.js` files
* Exports a cloud function matching the file name
* Author: David King
* Edited: Tarik Huber
* Based on this thread:
* https://github.com/firebase/functions-samples/issues/170
*/
const glob = require("glob");
const camelCase = require("camelcase");
const files = glob.sync('./**/*.f.js', { cwd: __dirname, ignore:
'./node_modules/**'});
for(let f=0,fl=files.length; f<fl; f++){
const file = files[f];
const functionName = camelCase(file.slice(0, -5).split('/').join('_')); //
Strip off '.f.js'
if (!process.env.FUNCTION_NAME || process.env.FUNCTION_NAME ===
functionName) {
exports[functionName] = require(file);
}
}
This index.js file is a way to organize your firebase cloud functions that I found on https://postmarkapp.com/blog/sending-transactional-emails-via-firebase-and-cloud-functions that got it from https://codeburst.io/organizing-your-firebase-cloud-functions-67dc17b3b0da
Thank you for your help!

It sounds like you need to run this from your functions directory:
npm install glob
And probably also
npm install camelcase
You can't require a module without first installing it into your project like this.

Related

Why nodejs didnt require p-queue?

I want to use p-queue npm nodejs. But it get me error. Help me why it was and how to fix it? Here my code:
const shell = require("shelljs");
const async = require("async");
const fs = require("fs");
const path = require("path");
const { default: PQueue } = require("p-queue");
const queue = new PQueue({ concurrency: 1 });
And this error:
internal/modules/cjs/loader.js:1080
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:
C:\Users\Feruz\Desktop\codeMaster\judge\node_modules\p-queue\dist\index.js
require() of ES modules is not supported. require() of
C:\Users\Feruz\Desktop\codeMaster\judge\node_modules\p-queue\dist\index.js
from C:\Users\Feruz\Desktop\codeMaster\judge\judge.js is an ES module
file as it is a .js file whose nearest parent package.json contains
"type": "module" which defines all .js files in that package scope as
ES modules. Instead rename index.js to end in .cjs, change the
requiring code to use import(), or remove "type": "module" from
C:\Users\Feruz\Desktop\codeMaster\judge\node_modules\p-queue\package.json.
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Users\Feruz\Desktop\codeMaster\judge\judge.js:5:29)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14) { code: 'ERR_REQUIRE_ESM' } [nodemon] app crashed - waiting for file
changes before starting...

NODE [ERR_MODULE_NOT_FOUND]: Cannot find module './config/app'

FIXED, the names of the folders in my pc changed when uploaded to github
So, my app looks something like this
├── config
| ├── app.js
|
└── index.js
app.js
Here i have all my env variables, so I have a central point from which to extract them
require('dotenv').config()
module.exports = {
appPort: process.env.PORT,
appUrl: process.env.APP_URL,
appKey: process.env.APP_KEY,
frontendUrl: process.env.FRONTEND_URL
}
index.js
central file of the server
const express = require('express')
const cors = require('cors')
const app = express()
const { appPort, frontendUrl } = require('./config/app')
app.listen(appPort , () => {
console.log(`Server listening on port ${appPort}`);
})
The problem here is that when i deploy it and starts the server, it throws me error
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module './config/app'
Require stack:
- /app/index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/app/index.js:8:34)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/app/index.js' ]
}
and i really don't know why, because it works perfectly fine in my pc
the code for the whole project is actually much bigger, but only this files are the ones throwing me errors
Hope someone can help see what is happening that throws that error

Error: Cannot find module 'firebase/app' When deploying google cloud function

Basically I am trying to have a firebase function simply output values in a document, I have only achieved this locally... but the function won't deploy to the cloud. Below is the function
const functions = require('firebase-functions');
const firebase = require('firebase/app');
require('firebase/firestore');
const firebaseConfig = {
projectId: "covid-info-bw"
};
const db = firebase
.initializeApp(firebaseConfig)
.firestore()
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase! Dawg I am beast");
});
exports.getData = functions.https.onRequest((req, res) => {
const docRef = db.collection('FunctionTest').doc('Cm38kBYShNnyuLVpizcy');
const getDoc = docRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document!');
return res.send('Not Found')
}
console.log(doc.data());
return res.send(doc.data());
})
.catch(err => {
console.log('Error getting document', err);
});
This is a snippet from the package.json
"dependencies": {
"#firebase/app": "^0.6.1",
"#firebase/firestore": "^1.14.0",
"#google-cloud/firestore": "^3.7.4",
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.0"
},
This is the error I am getting from running " firebase deploy --only functions:getData,functions:helloWorld"
λ firebase deploy --only functions:getData,functions:helloWorld
=== Deploying to 'covid-info-bw'...
i deploying functions
Running command: npm --prefix "%RESOURCE_DIR%" run lint
> functions# lint M:\VueAdventures\covid-info-bw\functions
> eslint .
+ functions: Finished running predeploy script.
i functions: ensuring necessary APIs are enabled...
+ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (41.45 KB) for uploading
+ functions: functions folder uploaded successfully
i functions: current functions in project: getData(us-central1), helloWorld(us-central1)
i functions: uploading functions in project: helloWorld(us-central1), getData(us-central1)
i functions: updating Node.js 8 function helloWorld(us-central1)...
i functions: updating Node.js 8 function getData(us-central1)...
! functions[getData(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'firebase/app'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/srv/index.js:2:18)
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)
! functions[helloWorld(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'firebase/app'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/srv/index.js:2:18)
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)
Functions deploy had errors with the following functions:
getData
helloWorld
To try redeploying those functions, run:
firebase deploy --only functions:getData,functions:helloWorld
To continue deploying other features (such as database), run:
firebase deploy --except functions
Error: Functions did not deploy properly.
You're not supposed to use the Firebase client SDKs for backend code. What you're trying to do can be achieved using only the Google Cloud Firestore SDK (#google-cloud/firestore) or the Firebase Admin SDK (firebase-admin, which just writes the Cloud SDK). Just remove the Firebase client SDKs (beginning with #firebase) and use only the backend SDKs you prefer.

GDAX API ReferenceError: suite is not defined

I am new to node.js and I am trying to use the GDAX API. I downloaded all of the programs and followed the instructions. I have not edited any of the .js files and I am just trying to run one of the provided test files.
npm outdated has all dependencies with their Wanted version and npm list has all dependencies in the green as the tree structure.
I use node public_client.js to run the test file.
Note all of the code was cloned from a git repository.
Here is an image of the error:
ERROR (Not enough history to embed images)
Here is the error in terminal:
Alecs-MacBook-Pro:tests Alec$ node public_client.js
/Users/Alec/Documents/Trading/gdax-node/tests/public_client.js:9
suite('PublicClient');
^
ReferenceError: suite is not defined
at Object.<anonymous> (/Users/Alec/Documents/Trading/gdax-node/tests/public_client.js:9:1)
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.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3
Here are the first nine lines of the file:
var assert = require('assert');
var nock = require('nock');
var Gdax = require('../index.js');
var publicClient = new Gdax.PublicClient();
var EXCHANGE_API_URL = 'https://api.gdax.com';
suite('PublicClient');
Here is the index file:
var PublicClient = require('./lib/clients/public.js');
var WebsocketClient = require('./lib/clients/websocket.js');
var AuthenticatedClient = require('./lib/clients/authenticated.js');
var Orderbook = require('./lib/orderbook.js');
var OrderbookSync = require('./lib/orderbook_sync.js');
module.exports = exports = {
'PublicClient' : PublicClient,
'WebsocketClient' : WebsocketClient,
'AuthenticatedClient': AuthenticatedClient,
'Orderbook' : Orderbook,
'OrderbookSync' : OrderbookSync,
};
That package is using mocha to run the tests. They have a script entry ready for that, so just run npm test and voila.
The command is using is in the package.json:
"scripts": {
"test": "mocha --ui qunit --bail --reporter list tests/*.js"
}

Node and Require import error in Mocha?

I have a working Node application and I'm trying to add Mocha tests, but getting some odd import errors.
This is my file structure:
package.json
index.js
src/
chart.js
highcharts-options.js
test/
test_chart.js
This is what my chart.js file looks like:
var chartOptions = require('./highcharts-options');
var analyseChart = {
doSomething: function() { ... }
};
module.exports = analyseChart;
And this is what highcharts-options.js looks like:
var HighCharts = require('highcharts-browserify');
Highcharts.theme = { ... };
Currently I import everything from /src into a single index.js file, then bundle it with browserify, which works just fine, no errors in the application.
Now I want to start writing Mocha tests for the functions in /src.
This is my first stub in test_chart.js:
var chart = require('../src/chart');
chart.doSomething();
But when I run mocha, I get the following error:
Users/.../js/src/highcharts-options.js:11
Highcharts.theme = {
^
ReferenceError: Highcharts is not defined
at Object.<anonymous> (/Users/.../js/src/highcharts-options.js:11:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/.../js/src/chart.js:7:20)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/.../js/test/test_chart.js:1:75)
How can I fix this import error for Mocha?
var HighCharts = require('highcharts-browserify');
Highcharts.theme = { ... };
You have a spelling error.
High[cC]harts
How did you not notice this yourself even when you were told exactly what was wrong?
Some errors are so common that we ultimately stop trusting them, and start looking for other faults in our code without even investigating it.
This is especially true for the "someVar is undefined" error in javascript.
Takeaway:
Sometimes you just got to trust your error messages.

Categories