Deploy fails with "Cannot find module 'firebase-admin' " even though module is in the function's package.json

Hey all,

I normally have a db.js function, which uses firebase-admin to read/write.

Now when deploying (via git push) I keep getting deployment failures (repeatedly, always):

**Plugin "@netlify/plugin-functions-core" internal error**

Error: In file "/opt/build/repo/functions/db/db.js": Cannot find module 'firebase-admin' Require stack:

* /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/resolve.js
* /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/dependencies.js
* /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/main.js
* /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/plugin.js
* /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins/child/logic.js
* /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins/child/load.js
* /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins/child/main.js

My package.json is simple and looks like this:

{
  "name": "db",
  "version": "1.0.0",
  "description": "netlify functions:create - default template for node fetch function",
  "main": "db.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "netlify",
    "serverless",
    "js"
  ],
  "author": "Netlify",
  "license": "MIT",
  "dependencies": {
    "firebase-admin": "^9.0.0",
    "node-fetch": "^2.3.0"
  }
}

As you can see, firebase-admin is there as a dependency.

Any ideas why Netlify wouldn’t see it?

Thanks!

hmm. that is pretty weird. Lets do some sanity checks.

Have you tried to “clear cache and redeploy”?

Is your package.json getting committed to your repo? There have been times when people have had their .gitignore set up in such a way that it never got updated.

Thanks for the quick reply @perry .

  1. I have tried npm cache clean --force.

  2. I have deleted package-lock.json and done npm i again.

  3. Yes, I double-checked my package.json is getting committed to my repo.

will you try this for me if you haven’t already?

Ah! I see what you meant by clearing cache and redeploying.

I have now tried that, with the same result.

Any ideas about this @perry?

so the question I have (and I am not a firebase expert) is to whether we are actually not finding/downloading the package or whether compilation of the project is failing because something about how the module is getting imported into the code isn’t right. Sometimes compilation fails for things like this:

I’m going to ask an engineer who knows a bit more about this than i do to take a look, also.

Good question. At the moment I’m not sure there’s anything special about the firebase-admin admin package, and I successfully use it in Netlify functions in my other projects. That’s why I’m confused why it’s not working in this case.

It would be great to check with an engineer for sure. Looking forward to your updates. Thanks!

Hi @question_asker,

I presume that you have a package.json in your function folder. Note that the buildbot won’t automatically npm i or yarn install. You’ll need to do that as part of your build command. Could you try something like cp function/db && npm i && cd ../.. && npm run build and see if that helps? (You’d probably want to extrapolate that command to fit your needs)

Correct @Dennis. I have a package.json in my function folder. It works when testing locally.

I’m confused re: your second question: in my experience, after I create a function using netlify functions:create foo and then npm i package-name to install a package, I can require that package within the functions index.js file, and then when building the project as a whole, that function’s packages are automatically built by Netlify. This workflow has been working for me in the recent months I’ve been using Netlify.

Could you please confirm that builtbot does not actually npm i the packages listed as dependencies in a function’s package.json file before running the function?

If you are using the cli to deploy, then you aren’t using our buildbot at all. This bundling process is done at deploy time and not at runtime, so I think perhaps I wasn’t clear. When I mentioned that function dependencies aren’t automatically installed, I assumed you deployed via a linked repo on your site and not via the cli, which use different code to bundle up your function.

That said, can you point to your site directly so I can take a look to see how your site is being deployed rather than guessing?

This may help https://medium.com/@saphidev/use-firebase-admin-with-netlify-lambda-functions-free-483d3b390e3a