Function - Cannot find module "airtable"

Hello, trying to set up a lambda function @ https://development--techinterviewcoach.netlify.app/

The function is at https://development--techinterviewcoach.netlify.app/.netlify/functions/proxy - it’s an entry point that, if all is good, calls other functions stored in netlify/functions/zaps

The function imports a separate js file I wrote import AirtableClient from "./utils/airtable-client.mjs"; as a single point of entry to Airtable.

Running things locally, everything is well - I am able to invoke localhost:8888/.netlify/functions/proxy, pass the appropriate headers and body and get the expects input/ouputs and actions in the db

yet, when I push to netlify and invoke the function with the same headers and body, I get the following error:

Jan 1, 04:11:03 AM: ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'airtable'\nRequire stack:\n- /var/task/proxy.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'airtable'","Require stack:","- /var/task/proxy.mjs"," at _loadUserApp (file:///var/runtime/index.mjs:1087:17)"," at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)"," at async start (file:///var/runtime/index.mjs:1282:23)"," at async file:///var/runtime/index.mjs:1288:1"]}

I’ve tried modifying the netlify.toml to include

[functions]
  external_node_modules = ["airtable"]

but it didn’t help

Is there a reproduction repo that we can try?

1 Like

Making one - working on it

fix: Netlify Forums #135891 by hrishikesh-k · Pull Request #1 · andreadellacorte/example-hello-world-functions

Seems to be happening due to your mixed usage of CJS and ESM.

Thanks! That fixed it.

Any idea why it worked locally? I’d like my local tests (I run netlify dev and then call the function on localhost:8888) to also fail, so I can avoid this in the future.

My guess for that would be the Node.js module resolution logic. By default, Node.js tries to look for a module in the parent directory untill it either finds it or reaches the root of the file system. Locally it would find the module in your root level node_modules directory. However, when the Function gets deployed, that folder is not included causing this issue.