Serverless Functions - Runtime.ImportModuleError. Works perfect locally but fails require() when deployed

Greetings, I love Netlify, and I have this hobby site

My project has 3 serverless functions that work perfectly in testing. I installed the netlify-cli locally and got everything working.

The functions require(‘faunadb’), however the Function Log shows:

{“errorType”:“Runtime.ImportModuleError”,“errorMessage”:"Error: Cannot find module ‘faunadb’\nRequire stack:\n- /var/task/netlify/functions/getContestants.js\n- /var/task/getContestants.js\n- }

Unknown application error occurred

So fauna is installed locally but can’t be found in the serverless function?

I have it in the package.json:
“name”: “vs-probst”,
“private”: true,
“version”: “0.0.0”,
“type”: “module”,
“scripts”: {
“dev”: “vite”,
“build”: “vite build”,
“preview”: “vite preview”,
“netlify”: “netlify”
“dependencies”: {
“faunadb”: “^4.8.0”,
“netlify-cli”: “^16.4.2”,
“react”: “^18.2.0”,
“react-dom”: “^18.2.0”,
“react-router-dom”: “^6.8.1”


I have found several other discussions with similar problems but I’m unable to figure out what to do here.

I have another netlify project where there are no issues. I’m not smart enough to know if its a version problem or why else require(‘faunadb’) fails when deployed but works locally. It’s the same files.

Any tips or guidance greatly appreciated

Howdy @gp-xyz and welcome! :cowboy_hat_face:

This looks similar to Problem with FaunaDB function- import failure. Have you tried the solution in that post?

FYI, answered my own question: I needed to have a package.json file listing faunadb as a dependency, and then I needed to specify a build command that runs npm install. Once I did that, I got past the dependency issue (having another issue with the query, but that’s entirely on me).

thank you audrey, I see that but don’t understand the answer. I have been asking chatgpt about the answer but I can’t solve it.

It says to include it as a dependency in package.json, which I have and have showed. I modified my netlify.toml file to be

  command = "npm install && npm run build"
  functions = "netlify/functions"

I have redeployed and have the same problem. I have cleared cache and rebuilt again but same problem.

If there is one thing I’m sure of, its chatGPT can’t help me as I’ve asked it every possible thing I can think of.

Any further guidance appreciated, thanks

Hiya @gp-xyz ! I went to try to take a look but as far as I can tell you seem to have removed or renamed the site you reference. I see a similarly named site in your team that has successful deploys - did you manage a fix in the meantime?

Thanks for the reply. I gave up and switched the implementation because I couldn’t figure it out. I plan on starting from scratch in the future and hopefully it somehow magically works then.