MySQL Package not found after deploy

I have read many posts around functions and package json’s, node_module installs etc. However after trying every suggestion, nothing works.

Issue:


I am running a lambda function with apollo-server-lambda, graphql, and typeorm.
Every function works fine in “netlify dev” and calls are made etc. No issues.

However, once I deploy to netlify it seems to remove the MySQL package that typeorm is reliant on as I am connecting to a MySQL database. (I have tried the serverless-typeorm and serverless-mysql packages too).

My file structure is:

Functions ->
                - graphql.js
                ->bundle ->
                                   built files from Server that graphql function points to (tried from netlify build and build and commit for netlify).
Server ->
             packageJson
             Node_Modules
             server.js - (This requires typeorm, MySQL, and other packages, other packages seem to work?)
Src(frontend)
PackageJson (I have tried including all deps here without the server packagejson too)
Node_Modules

I have deployed to App ID: c8917f89-5771-44ae-88f0-c1ebda658dbb
on a branch deploy on branch newDb.

I will be willing to give access to the git repo that is private to netlify support team members.

I have tried to build functions with netlify-lambda, tried not building at all, etc.
As mentioned before it works in dev and netlify dev

Hi, update… So it seems to work when I set the branch to be the production target branch and deploy to production? However, when it is a preview branch the package is not installed / doesnt work?
I think if this is the case the issue needs to be looking into as it would mean all my preview deploys will not work?

Hey @AaronP,
Where are you setting the production branch so that things work? That makes me wonder if there’s an node_env setting somewhere that’s set to ‘development’ in every other case, so not installing your mysql package whenever you expect. Could that be possible?

I’m also seeing in the file structure you listed about that there’s a server directory. It seems like you may already know this and have gotten your app running in some cases, but just in case: on Netlify, there’s no server at runtime, so you won’t be able to run a database on Netlify:

Hi Jen!

Thank you for the reply, I actually forgot to update the post. I did solve this by trial and error. I ended up requiring the MySQL package in every single function file and then it worked. Removed one by one until found the file it needed to be in, however, when running netlify dev it seemed to work from another file which still baffled me. However, now it is working!

Sorry to not update and waste your time replying, however if anyone else has this issue, I recommend having a functions own node_modules and requiring in each function file to find out which one needs it haha

Trial and error FTW :trophy: Appreciate you sharing your process so other folks can hopefully save some time/effort :slight_smile: