Use Node-Fetch in TypeScript with CloudFunctions - node.js

So Im using Cloud Functions from Firebase with TypeScript
I want to import node-fetch like:
import * as fetch from "node-fetch";
but it doesn't import correctly:
when I try to use fetch like this:
fetch("https://payments.sandbox.braintree-api.com/graphql", {
method: "POST",
headers: {
"Authorization":
"bnIzdm5nZHlqempjNnQ3bTo0ZjMxYjQ5YjA2MDNjN2RkMjZhM2UyMGE3M2E3MWVlNw==",
"Braintree-Version": "2022-08-13",
"Content-Type": "application/json",
},
body: JSON.stringify({
query,
}),
})
I get following problem:
This expression is not callable.
Type 'typeof import(".../node_modules/node-fetch/#types/index")' has no call signatures.
I was wondering whether I need to add a dependency after running:
npm install node-fetch
because in my package.json
{
"name": "functions",
"scripts": {
"lint": "eslint --ext .js,.ts .",
"build": "tsc",
"build:watch": "tsc --watch",
"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": "16"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^10.0.2",
"firebase-functions": "^3.18.0"
},
"devDependencies": {
"#typescript-eslint/eslint-plugin": "^5.12.0",
"#typescript-eslint/parser": "^5.12.0",
"eslint": "^8.9.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-import": "^2.25.4",
"firebase-functions-test": "^0.2.0",
"typescript": "^4.5.4"
},
"private": true
}
there is no node-fetch dependency
if this is the case how can I fix my problem?

As mentioned here, you have to add this dependency explicitly in the package.json, as node-fetch is not a built-in module and will need installation.
You can run npm install node-fetch on your local machine, but the Cloud Functions deploy doesn't work this way.
HTH, else let me know.

Related

Vue.js app is showing loading blank screen hosted on firebase

I am actually a flutter developer and have configured an existing project whose admin panel is built in VUE.js and mobile app is built in flutter. I have configured this app many times for clients but can't do it for the last time.
This hosted VUE.app is making problem and this is not. Both's code are same except some of the library changes however both were deployed with some of the deprecation warnings to firebase ( I will provide those warnings if necessary).
Admin panel has two parts i.e BACKEND & FRONTEND
Backend:
Index.js where the api cloud function lies which is deployed on firebase-functions.
const functions = require('firebase-functions');
const api = require('./src/api');
const runtimeOpts = {
timeoutSeconds: 300,
memory: '1GB',
};
exports.api = functions
.runWith(runtimeOpts)
.https.onRequest(api);
And this is the api's index being called from above index.js
const express = require('express');
const cors = require('cors');
const app = express();
const authFirebaseService = require('../auth/authFirebaseService');
const authMiddleware = require('../auth/authMiddleware');
const {
init: databaseInit,
middleware: databaseMiddleware,
} = require('../database/databaseInit');
const bodyParser = require('body-parser');
databaseInit().catch((error) => console.error(error));
authFirebaseService.init();
app.use(databaseMiddleware);
app.use(authMiddleware);
app.use(bodyParser.json());
app.use(cors({ origin: true }));
const routes = express.Router();
require('./auditLog')(routes);
require('./auth')(routes);
require('./iam')(routes);
require('./settings')(routes);
require('./customers')(routes);
require('./suppliers')(routes);
require('./staff')(routes);
require('./products')(routes);
require('./brands')(routes);
require('./categories')(routes);
require('./expenses')(routes);
require('./expenseCategory')(routes);
require('./assets')(routes);
require('./devices')(routes);
require('./notice')(routes);
require('./sales')(routes);
require('./purchases')(routes);
require('./returns')(routes);
require('./stockAdjustments')(routes);
require('./units')(routes);
require('./giftCard')(routes);
require('./coupon')(routes);
require('./damages')(routes);
require('./leave')(routes);
require('./leaveType')(routes);
require('./allowanceAndDeduction')(routes);
require('./payroll')(routes);
require('./attendance')(routes);
require('./taxClass')(routes);
require('./todo')(routes);
require('./documents')(routes);
require('./paymentMethods')(routes);
require('./documentTypes')(routes);
require('./memos')(routes);
require('./currency')(routes);
require('./holiday')(routes);
require('./stall')(routes);
require('./variation')(routes);
require('./productLogs')(routes);
require('./stockTransfer')(routes);
require('./deviceSessions')(routes);
require('./productCount')(routes);
require('./productValue')(routes);
require('./register')(routes);
require('./commissions')(routes);
require('./transactions')(routes);
require('./priceChange')(routes);
app.use('/api', routes);
module.exports = app;
package.json:
{
"name": "app-backend",
"description": "Backend",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "nodemon ./server.js",
"logs": "firebase functions:log",
"test": "nodemon --exec 'cross-env NODE_ENV=test mocha ./src/**/*.test.js --exit || exit 1'",
"predeploy:development": "firebase use development && firebase functions:config:set env.value=\"development\"",
"deploy:development": "firebase deploy --only functions",
"deploy:appengine:development": "gcloud app deploy app-engine.development.yaml --project <insert project id here>",
"predeploy:production": "firebase use production && firebase functions:config:set env.value=\"production\"",
"deploy:production": "firebase deploy --only functions",
"deploy:appengine:production": "gcloud app deploy app-engine.production.yaml --project <insert project id here>"
},
"dependencies": {
"cors": "2.8.5",
"express": "4.16.4",
"firebase-admin": "7.0.0",
"firebase-functions": "2.2.0",
"graphql": "14.1.1",
"graphql-fields": "2.0.1",
"graphql-iso-date": "3.6.1",
"graphql-tools": "4.0.4",
"graphql-type-json": "0.2.1",
"lodash": "^4.17.15",
"moment": "2.24.0",
"nodemailer": "5.1.1"
},
"engines": {
"node": "16"
},
"private": true,
"devDependencies": {
"cross-env": "5.2.0",
"firebase-functions-test": "0.1.6",
"mocha": "^6.2.2",
"node-mocks-http": "1.7.3",
"nodemon": "1.18.10"
},
"version": "1.0.0",
"main": "index.js",
"author": "Bilal Saeed",
"license": "ISC"
}
Frontend:
package.json
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"scripts": {
"start": "npm run start:localhost",
"start:localhost": "vue-cli-service serve --mode localhost --port 8081",
"build:localhost": "vue-cli-service build --mode localhost",
"start:development": "vue-cli-service serve --mode development --port 8081",
"build:development": "vue-cli-service build --mode development",
"deploy:development": "firebase use development && npm run build:development && firebase deploy --only hosting,storage",
"start:production": "vue-cli-service serve --mode production --port 8081",
"build:production": "vue-cli-service build --mode production",
"deploy:production": "firebase use production && npm run build:production && firebase deploy --only hosting,storage",
"lint": "vue-cli-service lint"
},
"dependencies": {
"axios": "^0.19.0",
"chart.js": "2.7.3",
"element-ui": "2.5.4",
"file-saver": "2.0.1",
"filesize": "4.1.2",
"firebase": "^9.9.1",
"lodash": "^4.17.15",
"md5": "2.2.1",
"moment": "2.24.0",
"nprogress": "0.2.0",
"portal-vue": "2.1.6",
"qs": "6.7.0",
"uuid": "7.0.3",
"vue": "2.6.7",
"vue-router": "3.0.2",
"vuex": "3.1.0",
"xlsx": "0.14.1",
"yup": "0.26.10"
},
"devDependencies": {
"#vue/cli-plugin-babel": "3.4.1",
"#vue/cli-plugin-eslint": "3.4.1",
"#vue/cli-service": "3.4.1",
"#vue/eslint-config-prettier": "4.0.1",
"babel-eslint": "10.0.1",
"eslint": "5.14.1",
"eslint-plugin-vue": "5.2.2",
"vue-cli-plugin-element": "1.0.1",
"vue-template-compiler": "2.6.7"
},
"main": ".eslintrc.js",
"author": "Bilal Saeed",
"license": "ISC",
"description": ""
}
I have no experience with configuration of VUE.js app, just read the provided manual for installation of software and whenever I faced any bug during configuration, I googled it. But in this case, I'm not having any error.

Google cloud function with puppeteer does not work, even though it works in firebase functions emulator

So I made this function. It works correctly in firebase functions emulator, but when i use firebase deploy --only functions, I am getting errors like the ones on the picture. Any idea what I am doing wrong?
Here is my package.json
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions -P default",
"serve-dev": "npm run build && firebase serve --only functions -P staging",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions -P default",
"deploy-dev": "firebase deploy --only functions -P staging",
"logs": "firebase functions:log"
},
"main": "lib/index.js",
"dependencies": {
"#google-cloud/functions-framework": "^1.7.1",
"#types/node-fetch": "^2.3.3",
"firebase-admin": "^9.4.1",
"firebase-functions": "^3.11.0",
"golang": "^0.1.5-stable",
"node-fetch": "^2.5.0",
"puppeteer": "5.4.1",
"xml2js": "^0.4.19"
},
"devDependencies": {
"tslint": "^5.20.1",
"typescript": "^3.9.2"
},
"private": true,
"engines": {
"node": "10"
}
}
Here is the code of the function, it is in TypeScript, which after building becomes js ofc:
export const RecurringTradingUpdate = functions.https.onRequest(async (request, response) => {
const browser = await puppeteer.launch({
headless: true
});
const page = await browser.newPage();
await page.goto('website');
await page.waitFor(2000);
//await page.click('input[type=password]');
await page.click('#inputPassword');
await page.keyboard.sendCharacter('password')
//await page.click('button[type=submit]');
await page.click('#clickPassword');
await page.waitFor(2000);
//await page.click('button[type=submit]');
await page.click('#submit');
await page.waitFor(120000)
response.send("All done.")
});

Function deployment hangs at "all necessary APIs are enabled"

When deploying the Cloud Functions to the Firebase Cloud, they take a very long time right after the log "functions: all necessary APis are enabled".
I am deploying 6 functions.
I am using node 10 since using node 8 in combination with the URL module caused some errors that went away using node 10.
the package.json:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"babel-eslint": "^10.0.1",
"cheerio": "^1.0.0-rc.2",
"firebase-admin": "~7.0.0",
"firebase-functions": "^2.3.0",
"lighthouse": "^5.1.0",
"ping": "^0.2.2",
"puppeteer": "^1.17.0",
"puppeteer-extra": "^2.1.3",
"request": "^2.88.0",
"request-promise-native": "^1.0.7",
"url": "^0.11.0"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-plugin-promise": "^4.0.1"
},
"engines": {
"node": "10"
},
"private": true
}
The deployment used to go much faster, but recently it started behaving differently.
I dont know when excactly it happened.
My questions are:
Does anyone know what the issue might be?
If no, does anyone know how I would go about 'debugging' this issue?
Thank you.

ES5: 'use strict' within dependency fails to be minified by Heroku

I am trying to deploy on Heroku, where I'm given the following error:
remote: Failed to minify the code from this file:
remote: ./node_modules/webhoseio/webhoseio.js:13
Upon inspecting this dependency, I found that it uses the ES5 'use strict'; declaration. How can I have Heroku compile this dependency?
EDIT: Package.json file
{
"name": "stocks-app",
"version": "1.0.0",
"description": "Mern Demo",
"main": "server.js",
"scripts": {
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:prod": "babel-node server.js",
"start:dev": "concurrently \"nodemon --ignore 'client/*'\" \"npm run client\"",
"client": "cd client && npm run start",
"install": "cd client && yarn install",
"build": "cd client && npm run build",
"heroku-postbuild": "npm run build"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel": "^6.23.0",
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"concurrently": "^4.1.0",
"nodemon": "^1.18.9"
},
"dependencies": {
"alphavantage": "^1.2.0",
"axios": "^0.18.0",
"brain.js": "^1.6.0",
"cors": "^2.8.5",
"dotenv": "^6.2.0",
"express": "^4.16.3",
"if-env": "^1.0.4",
"mdbreact": "^4.8.5-patch.1",
"mongoose": "^5.4.0",
"newsapi": "^2.4.0",
"request": "^2.88.0",
"webhoseio": "^1.0.2"
}
}
I resolved this by turning off minification in my Webpack config. Instead of using the CRA-preconfigured Webpack ES-Lint loader, I installed the HTML-Loader and set loader to that. Following, I set minimize to false. Here's how:
module: {
rules: [
{
use: [
{
options: {
formatter: eslintFormatter,
eslintPath: require.resolve('eslint'),
minimize: false,
},
loader: require.resolve('html-loader'),
},
],}}

React Native package.json versions

I have a React Native project created a month ago, using create-react-native-app command.
The package.json contents are as follow:
{
"name": "TestApp2",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-native-scripts": "1.1.0",
"jest-expo": "~19.0.0",
"react-test-renderer": "16.0.0-alpha.12"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "node node_modules/jest/bin/jest.js --watch"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"expo": "^19.0.0",
"react": "16.0.0-alpha.12",
"react-native": "^0.46.1"
}
}
When I create a project today, the package.json is as follow:
{
"name": "NewApp",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-native-scripts": "1.5.0",
"jest-expo": "^21.0.2",
"react-test-renderer": "16.0.0-alpha.12"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "node node_modules/jest/bin/jest.js --watch"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"expo": "^21.0.0",
"react": "16.0.0-alpha.12",
"react-native": "^0.48.4"
}
}
Please note that the versions of react-native-scripts, jest-expo and expo are different. My question is, how can I upgrade the version? Should I just change the version number manually?
I tried to use:
$ npm install -g react-native-git-upgrade
$ react-native-git-upgrade
But it does not upgrade the versions.
I also tried:
$ npm update
$ react-native upgrade
But both commands don't update the versions.
Just manually update the versions in your package.json file and the re-run npm install.
Because you are using expo,
you can also use its upgrade functions.
So just run expo upgrade in the root directory, look here
expo doc
{
"react-native-scripts": "1.1.0",
"jest-expo": "~19.0.0",
},
try edit with new version,and run "rm -rf node_modules && npm install" in your project!

Resources