0

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?

Nikita
  • 477
  • 4
  • 14
  • Does this answer your question? [How to use node-fetch with Cloud Functions for Firebase. Can't deploy](https://stackoverflow.com/questions/69583511/how-to-use-node-fetch-with-cloud-functions-for-firebase-cant-deploy) – Dharmaraj Aug 28 '22 at 10:18
  • Have a look at this stackoverflow [link](https://stackoverflow.com/a/43708548/18265638) which might help – Sathi Aiswarya Aug 29 '22 at 10:44

1 Answers1

0

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.

Gourav B
  • 864
  • 5
  • 17
  • yeah my problem was that only version 2.6.1 can be used, but at the same time I dont want to use it because it has a high vulnerability risk :/ – Nikita Oct 19 '22 at 12:22