Zorgan
Zorgan

Reputation: 9143

Getting "Error: Cannot find module 'request'" even though I have added request module

I am trying to use request for my Cloud Function.

I have installed request package via npm install request. It sits in node_modules like every other package:

enter image description here

However when I try to upload my Cloud Function, I get the following error message:

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 'request'
    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:3:17)
    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)

Here's my index.js:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const request = require('request');

admin.initializeApp();
const db = admin.firestore();

function getFoursquarePlace(midPoint){
    let url = "https://api.foursquare.com/v2/venues/search?" +
    "ll=" + midPoint.latitude + "," + midPoint.longitude + "&client_id=" +
    client_id + "&client_secret=" + client_secret + "&v=" + version + "&radius=" + radius 
    + "&limit=" + limit + "&categoryId=" + categorySet;

    request(url, options, (error, res, body) => {
    if (error) {
        return console.log(error)
    }

    if (!error && res.statusCode === 200) {
        // do something with JSON, using the 'body' variable
            console.log("body: " + res.body);
        }
    });

Does anyone know what the problem may be? I've tried uninstalling and reinstalling request but still getting the error.

Edit: I've checked package-lock.json and the request package is in there:

"request": {
  "version": "2.88.0",
  "resolved": "https://registry.npmjs.org/request/-/request-2.88.0.tgz",
  "integrity": "sha512-NAqB3rk3LgxdsfHnTBNwN0E+lHDAJzu7m0328eY08z2/A0hg==",
  "requires": {
    "aws-sign2": "~0.7.0",
    "aws4": "^1.8.0",
    "caseless": "~0.12.0",
    "combined-stream": "~1.0.6",
    "extend": "~3.0.2",
    "forever-agent": "~0.6.1",
    "form-data": "~2.3.2",
    "har-validator": "~5.1.0",
    "http-signature": "~1.2.0",
    "is-typedarray": "~1.0.0",
    "isstream": "~0.1.2",
    "json-stringify-safe": "~5.0.1",
    "mime-types": "~2.1.19",
    "oauth-sign": "~0.9.0",
    "performance-now": "^2.1.0",
    "qs": "~6.5.2",
    "safe-buffer": "^5.1.2",
    "tough-cookie": "~2.4.3",
    "tunnel-agent": "^0.6.0",
    "uuid": "^3.3.2"
  }
},

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"
  },
  "engines": {
    "node": "8"
  },
  "dependencies": {
    "firebase-admin": "^8.0.0",
    "firebase-functions": "^3.1.0",
    "geofirestore": "3.3.1"
  },
  "devDependencies": {
    "eslint": "^5.12.0",
    "eslint-plugin-promise": "^4.0.1",
    "firebase-functions-test": "^0.1.6"
  },
  "private": true
}

Upvotes: 3

Views: 5654

Answers (2)

roshni batra
roshni batra

Reputation: 39

I was facing the same issue , it turned I was missing to add [email protected] in dependency in package.json file though it was present in devDependencies

package.json

In case this doesn't resolve try running

npm audit fix --force

firebase deploy --only functions

Upvotes: 1

mohammad javad ahmadi
mohammad javad ahmadi

Reputation: 2081

This can happen when you "npm install" a dependency to the wrong folder, as I have just realised I did. I have a Vue project in the "src" subdirectory, and an index.js (Firebase cloud function) in the "functions" subdirectory.

Dependencies of the Vue project must be installed with "npm install" in the main directory. In contrast, dependencies of the Firebase cloud function must be installed with "npm install" being run in the "functions" subdirectory. I had accidentally installed the dependency in the main directory, and was breaking my head trying to understand why it was complaining that it couldn't find the dependency (googleapis) until I realised what I had done. The fix was of course to install it in the right place (so it showed up in the right package.json), and uninstall it from the wrong place for neatness.

npm uninstall request //removes the module from node_modules, but not package.json or npm uninstall --save request
npm cache clear --force
npm install --save --save-exact [email protected]       
firebase deploy --only functions

Upvotes: 4

Related Questions