Netlify Functions with faunaDB-graphql backend returns a 502 in production only

Hello,

Trying to get a normal Apollo GraphQL serverless backend working with netlify functions. But when I try to run my Query in production it just returns with a 502 error in production only. Works just fine in development.

github repo: https://github.com/rptrainor/mntownhall

Any ideas? Thank you in advance.

log error: POST https://mntownhall.netlify.com/.netlify/functions/backend 502
netlify instance name: mntownhall.netlify.com

Looks like a dependency is missing in the function

{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'apollo-server-lambda'\nRequire stack:\n- /var/task/backend.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","trace":["Runtime.ImportModuleError: Error: Cannot find module 'apollo-server-lambda'","Require stack:","- /var/task/backend.js","- /var/runtime/UserFunction.js","- /var/runtime/index.js","    at _loadUserApp (/var/runtime/UserFunction.js:100:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1158:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)","    at Module.load (internal/modules/cjs/loader.js:1002:32)","    at Function.Module._load (internal/modules/cjs/loader.js:901:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)","    at internal/main/run_main_module.js:18:47"]}

Which is odd because its here: https://github.com/rptrainor/mntownhall/blob/master/functions/backend/package.json#L21

This is perplexing :thinking:

You say it works elsewhere?

Hi David,

Thanks for the quick response! Yes, the query works just fine in development with netlify dev , but returns the 502 error in production.

To reproduce the error check the console log when you visit https://mntownhall.netlify.com/susankent

the Netlify functions folder backend was set up following the steps used in this tutorial: https://youtu.be/vrSoLMmQ46k?t=3996 at around the 1:06:35 mark

It seems like the dependancies aren’t being installed in the functions folder.

Can you update your build command https://github.com/rptrainor/mntownhall/blob/master/netlify.toml#L3 to: yarn build && cd functions/backend && npm install

Alternatively the Netlify Build Plugins: add powerful capabilities to every build should automatically install function dependancies for you and you won’t need to change your build command.

Totally worked! Thank you!

1 Like