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

adapter-vercel — external option breaks dependecies #5024

Closed
acarl005 opened this issue May 22, 2022 · 3 comments · Fixed by #4969
Closed

adapter-vercel — external option breaks dependecies #5024

acarl005 opened this issue May 22, 2022 · 3 comments · Fixed by #4969
Labels
bug Something isn't working p1-important SvelteKit cannot be used by a large number of people, basic functionality is missing, etc. pkg:adapter-vercel Pertaining to the Vercel adapter
Milestone

Comments

@acarl005
Copy link

acarl005 commented May 22, 2022

Describe the bug

I have deployed a SvelteKit app to Vercel using @sveltejs/adapter-vercel. My serverless function is throwing this error:

[POST] /query                           00:15:09:41

2022-05-22T04:15:09.519Z	undefined	ERROR	
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'pgsql-parser' imported from /var/task/index.js
    at new NodeError (node:internal/errors:371:5)
    at packageResolve (node:internal/modules/esm/resolve:930:9)
    at moduleResolve (node:internal/modules/esm/resolve:976:18)
    at defaultResolve (node:internal/modules/esm/resolve:1078:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:79:40)
    at link (node:internal/modules/esm/module_job:78:36) 
{  code: 'ERR_MODULE_NOT_FOUND'}
RequestId: f8a6e809-859e-4f10-ac26-14f5194f92fa 
Error: Runtime exited with error: exit status 1 Runtime.ExitError

I definitely have "pgsql-parser" listed in my dependencies in my package.json. My svelte.config.js looks something like this:

import fs from "fs"
import path from "path"
import vercel from "@sveltejs/adapter-vercel"

const packageJson = JSON.parse(fs.readFileSync("./package.json", "utf8"))

/** @type {import("@sveltejs/kit").Config} */
const config = {
  kit: {
    adapter: vercel({
      edge: false,
      external: Object.keys(packageJson.dependencies),
      split: false
    }),
    vite: {
      // stuff...
    }
  }
}

export default config

I have excluded the runtime dependencies from the bundle. This is necessary because 1 of my dependencies is the Postgres node.js driver,"pg", which relies on native addons, and it cannot be bundled. The Vercel docs say that the dependencies will get automatically installed. However, it seems that my function is unable to find my dependencies.

How can I make sure my dependencies are available at function runtime without bundling?

Reproduction

I have a simpler reproduction here. When I deploy that to Vercel, it crashes in the hook because '@lukeed/uuid isn't found.

Logs

[GET] /favicon.ico
01:02:12:22

2022-05-22T05:02:12.310Z	3fbcc9e4-eb1e-4e7d-9ab0-69e65ab19948	ERROR
	Unhandled Promise Rejection
{
  "errorType": "Runtime.UnhandledPromiseRejection",
  "errorMessage": "Error: Cannot find module '@lukeed/uuid'\nRequire stack:\n- /var/task/index.js",
  "reason": {
    "errorType": "Error",
    "errorMessage": "Cannot find module '@lukeed/uuid'\nRequire stack:\n- /var/task/index.js",
    "code": "MODULE_NOT_FOUND",
    "requireStack": [
      "/var/task/index.js"
    ],
    "stack": [
      "Error: Cannot find module '@lukeed/uuid'",
      "Require stack:",
      "- /var/task/index.js",
      "    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)",
      "    at Function.Module._load (node:internal/modules/cjs/loader:778:27)",
      "    at Module.require (node:internal/modules/cjs/loader:1005:19)",
      "    at require (node:internal/modules/cjs/helpers:102:18)",
      "    at .svelte-kit/output/server/chunks/hooks-0bab96cd.js (/var/task/index.js:5495:19)",
      "    at __init (/var/task/index.js:8:56)",
      "    at /var/task/index.js:8166:59",
      "    at processTicksAndRejections (node:internal/process/task_queues:96:5)",
      "    at async Server.respond (/var/task/index.js:8166:23)",
      "    at async Server.serverless_default (/var/task/index.js:8228:20)"
    ]
  },
  "promise": {},
  "stack": [
    "Runtime.UnhandledPromiseRejection: Error: Cannot find module '@lukeed/uuid'",
    "Require stack:",
    "- /var/task/index.js",
    "    at process.<anonymous> (file:///var/runtime/index.mjs:775:15)",
    "    at process.emit (node:events:532:35)",
    "    at emit (node:internal/process/promises:133:20)",
    "    at processPromiseRejections (node:internal/process/promises:260:27)",
    "    at processTicksAndRejections (node:internal/process/task_queues:97:32)"
  ]
}
 	Unknown application error occurred

System Info

Vercel serverless function

Severity

blocking all usage of SvelteKit

Additional Information

No response

@mrkishi
Copy link
Member

mrkishi commented May 22, 2022

As far as I understand this is currently unsupported as everything needs to get bundled with esbuild.

Fortunately #4969 should fix this soon.

@mrkishi mrkishi linked a pull request May 22, 2022 that will close this issue
6 tasks
@mrkishi mrkishi added bug Something isn't working pkg:adapter-vercel Pertaining to the Vercel adapter p1-important SvelteKit cannot be used by a large number of people, basic functionality is missing, etc. labels May 22, 2022
@mrkishi mrkishi added this to the 1.0 milestone May 22, 2022
@acarl005
Copy link
Author

acarl005 commented May 22, 2022

Looking into this a bit deeper, I notice that a package.json gets generated for the function in the vercel output

npm run build
cat .vercel_build_output/functions/node/render/package.json

{"type":"module"}

Would it be possible to add "dependencies" to the package.json in order to make those NPM packages available at runtime? Seems like an easier lift than removing esbuild altogether. Maybe the external from the adapter-vercel options in svelte.config.js could be automatically added to the package.json dependencies? Or maybe there could be a separate option for the adapter, something like runtimeDependecies which gets added to to the generated package.json dependencies for the function?

@Rich-Harris
Copy link
Member

Adding dependencies to package.json won't do anything — the docs you linked above don't apply to the build output API, which is lower-level than functions written inside the api directory.

#4969 is the way to fix this, we just need to wait for a new release of turborepo which fixes a bug around symlinks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p1-important SvelteKit cannot be used by a large number of people, basic functionality is missing, etc. pkg:adapter-vercel Pertaining to the Vercel adapter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants