Missing dependency in serverless function

I have a similar issue to https://community.netlify.com/t/using-require-to-include-a-relative-module-in-netlify-functions-on-node/4177, but it is around the discovery of dependencies from a relative import.

I’m working on bringing Netlify support to Bison. Under the hood it uses Next.js and I was trying to leverage the next-on-netlify package. My api function imports a relative module outside the function directory that has a dependency on @nexus/schema.

At run-time it fails with the following error:

After doing a bit of research and trying multiple approaches, it appears that the file locations are the problem. But, only from the standpoint that the Netlify builder does not include the dependency. Is there anything I can do outside of manually zipping the file with https://github.com/netlify/zip-it-and-ship-it#usage?

I don’t want to move the modules into pages/api because I don’t want them to build as separate functions.

I’ve also tried explicitly requiring @nexus/schema in the pages/api/graphql.ts file, but that didn’t help.

Any suggestions?

hey @cball thanks for writing this up! definitely seen this before with nextjs and understand the frustration. all next-on-netlify really does is take the page that nextjs generates for any /api routes and wraps it to fit in a function. if you don’t want to try zipping the file, maybe try to work around nextjs (and next-on-netlify) and create your own netlify function yourself, if only to verify it works and isolate that nextjs’ bundled page is in fact the problem. if you want to try this (while using next-on-netlify), you’ll have to use the CLI, because next-on-netlify currently doesn’t support custom netlify functions out of the box (we’re working on it!).

you’ll have to

  1. netlify deploy --build
  2. copy your custom function into out_functions
  3. netlify deploy --prod

confused though about the BisonNetlify project/example because the README/actions talk about deploying to vercel?

Hey @lindsaylevine! Appreciate the reply.

Sorry for the confusion on the BisonNetlify example README. I’m trying to add Netlify support to the project, which is why you don’t see anything in the README / actions about Netlify just yet :slightly_smiling_face:.

I’ll see if I can make any headway with your suggestion. Since this works on Vercel, I don’t know that its a problem with the way Next.js is building, more that the Netlify builder is not recognizing the require statement because of the folder locations and therefore not installing the dependency?

ah right i gotcha! @ README

more that the Netlify builder is not recognizing the require statement because of the folder locations and therefore not installing the dependency

so you’re saying it works just fine running next dev and deploying to Vercel but fails running ntl dev and on Netlify production? i’ll try to poke around and see if anyone more intimate with Netlify functions/buildbot has any ideas.

and let me know if you make/made any progress with the custom functions suggestion :slight_smile:!