I’ve tried requiring shared utility-type modules that live outside my functions directory and get a Error: Cannot find module '../utils' message when the function is run after being deployed. Interestingly, the error is not thrown when running the function locally using netlify dev.
I was wondering if this is expected or if it should be possible to require modules that are outside the functions directory.
I got around my problem by putting the shared modules in a lib folder inside the functions directory. Are there any drawbacks to or recommendations against that approach?
I went a couple rounds on this issue too. Found that creating a shared folder in the root of functions is only accessible to the function if the function is not in a subfolder.
A shared js file is found if the folder structure looks like this:
correct. the reasoning for this is how netlify functions work under the hood - if a single js file, it works in context of the overall functions folder. if a folder, it is zipped up and deployed as a standalone function with no access to anything else.
to be honest i’m a bit lost on this. we need to document better and also provide better replication in Netlify Dev. i frankly dont have enough node-fu to figure out how to prevent requiring external files in Netlify Dev (functions: don't allow requiring shared/external folders · Issue #426 · netlify/cli · GitHub). and also have to think about how to document better. ideas welcome.