Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find packages imported by autoloaded plugins in Vercel Serverless Function #265

Open
2 tasks done
galiarmero opened this issue Sep 1, 2022 · 4 comments
Open
2 tasks done

Comments

@galiarmero
Copy link

Prerequisites

  • I have written a descriptive issue title
  • 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

  1. Fork this project: https://github.com/galiarmero/fastify-vercel-serverless
  2. Log in to Vercel web app using Github account
  3. In the dashboard, click Add New... then choose Project
  4. Under Import Git Repository, find the newly forked fastify-vercel-serverless and click Import
  5. Under *Configure Project, leave the fields as is and click Deploy
  6. 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:

  1. Apply the changes made in this PR: Manually load plugins and routes galiarmero/fastify-vercel-serverless#2
  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()

@mcollina
Copy link
Member

mcollina commented Sep 1, 2022

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.

@alexcroox
Copy link

alexcroox commented Dec 15, 2022

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

@ifritzler
Copy link

Hello guys and thank you for all your work! Do you think in near future supporting this for bundlers? Any alternatives to do this?

@mcollina
Copy link
Member

mcollina commented Dec 8, 2023

I would love if somebody could work on this, or is willing to sponsor the work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants