fastify-autoload
fastify-autoload copied to clipboard
Cannot find packages imported by autoloaded plugins in Vercel Serverless Function
Prerequisites
- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the bug has not already been reported
Fastify version
4.5.3
Plugin version
5.2.0
Node.js version
16.x
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
20.04
Description
I'm trying to deploy a Fastify app as a serverless function in Vercel. The function crashes when plugins are autoloaded but when they are individually loaded using fastify.register()
, it runs fine. Stacktrace shows that packages from node_modules are not found:
2022-09-01T03:18:51.555Z 09a47476-218f-4551-a748-ff23511f5cb2 ERROR Unhandled Promise Rejection
{
"errorType": "Runtime.UnhandledPromiseRejection",
"errorMessage": "Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'fastify-plugin' imported from /var/task/src/plugins/sensible.js",
"reason": {
"errorType": "Error",
"errorMessage": "Cannot find package 'fastify-plugin' imported from /var/task/src/plugins/sensible.js",
"code": "ERR_MODULE_NOT_FOUND",
"stack": [
"Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'fastify-plugin' imported from /var/task/src/plugins/sensible.js",
" at new NodeError (node:internal/errors:372:5)",
" at packageResolve (node:internal/modules/esm/resolve:954:9)",
" at moduleResolve (node:internal/modules/esm/resolve:1003:20)",
" at defaultResolve (node:internal/modules/esm/resolve:1218:11)",
" at ESMLoader.resolve (node:internal/modules/esm/loader:580:30)",
" at ESMLoader.getModuleJob (node:internal/modules/esm/loader:294:18)",
" at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:80:40)",
" at link (node:internal/modules/esm/module_job:78:36)"
]
},
"promise": {},
"stack": [
"Runtime.UnhandledPromiseRejection: Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'fastify-plugin' imported from /var/task/src/plugins/sensible.js",
" at process.<anonymous> (file:///var/runtime/index.mjs:1131:17)",
" at process.emit (node:events:539:35)",
" at emit (node:internal/process/promises:140:20)",
" at processPromiseRejections (node:internal/process/promises:274:27)",
" at processTicksAndRejections (node:internal/process/task_queues:97:32)"
]
}
In the index, the package that was unsuccessfully imported was fastify-plugin
but I also tried importing other npm packages on the plugin and had the same error. However, if the imported modules are from within the codebase, the error is not happening.
In contrast, I have verified that when plugins and routes are loaded via fastify.register()
, the error is not observed and the function is running fine. See Steps to Reproduce section.
Additionally, I was able to verify that the node_modules
folder exists in the root dir of the serverless environment.
Any leads on where to look at and how this can be fixed?
Steps to Reproduce
❌ To reproduce the crashing function when deployed in Vercel
- Fork this project: https://github.com/galiarmero/fastify-vercel-serverless
- Log in to Vercel web app using Github account
- In the dashboard, click Add New... then choose Project
- Under Import Git Repository, find the newly forked
fastify-vercel-serverless
and click Import - Under *Configure Project, leave the fields as is and click Deploy
- After depoy, you should see the screenshot of the failing app. Click that to open the error page and click check the logs
✅ To reproduce the successful deployment when plugins are autoloaded
Assuming the failing scenario above has been replicated:
- Apply the changes made in this PR: https://github.com/galiarmero/fastify-vercel-serverless/pull/2
- Under the PR checks, click Show all checks and open the Details of the Vercel deployment check
Expected Behavior
Packages from node_modules when imported inside the autoloaded plugins and routes, just like how it is when the plugins are loaded via fastify.register()
I'm not a Vercel functions user so I can't easily help. My guess is that Vercel is not bundling things correctly in this case.
Maybe @feugy knows somebody that can help.
Are you using any kind of bundler to reduce deployment size? This library will not work if you use a bundler so you have to go back to manually registering each plugin instead
Hello guys and thank you for all your work! Do you think in near future supporting this for bundlers? Any alternatives to do this?
I would love if somebody could work on this, or is willing to sponsor the work.