fastify / fastify/fastify-autoload

Cannot find packages imported by autoloaded plugins in Vercel Serverless Function

Open
#265 4 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
373
Forks
77
PR merge metrics
No merged PRs in 30d

Description

### 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:

```json
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. (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. (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: https://github.com/galiarmero/fastify-vercel-serverless/pull/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()`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.