[services] @vercel/backends makes an outputDirectory that holds the entrypoint the function root, so a pnpm workspace service cannot load
Environment
- Vercel Services (Beta), Vercel CLI 59.25.4 with
@vercel/backends8.0.2. The builder code indist/index.mjsis the same in 10.0.0, pinned by CLI 60.0.1. - pnpm 10 workspace. The service lives below the repository root (
root: "apps/api"). - NestJS 12 app, ESM (
"type": "module"inapps/api/package.json), Node 24.
Configuration
{
"services": {
"api": {
"root": "apps/api",
"framework": "nestjs",
"buildCommand": "pnpm build",
"outputDirectory": "dist"
}
},
"rewrites": [{ "source": "/(.*)", "destination": { "service": "api" } }]
}pnpm build runs nest build and writes apps/api/dist/main.js.
What happens
The build succeeds, but every request fails. The function log shows:
Warning: Failed to load the ES module: /var/task/main.js. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension. SyntaxError: Cannot use import statement outside a modulemaybeDoBuildCommandfindsmain.jsindistand globsdist/**relative todist. So the function'sfilePathMapputsmain.jsat the root (main.js -> apps/api/dist/main.js). The traced dependencies keep their repository paths (node_modules/.pnpm/…,apps/api/node_modules/…)./var/task/main.jstherefore has nopackage.jsonwith"type": "module"beside it, and its bare imports cannot resolve. A local copy of the function filesystem shows the same failure:ERR_MODULE_NOT_FOUND: Cannot find package 'reflect-metadata' imported from …/main.js.Leaving
outputDirectoryunset gives the same layout, because the builder then searchesdist,buildandoutputafter the build command.Naming the built file instead (
"entrypoint": "dist/main.js") is rejected before the build:Service "api" has entrypoint "dist/main.js" but that path does not exist.The builder itself finds the entrypoint after the build command (#17284), but the services resolver checks for it before the build.
Expected
- Either keep the output directory's files at their repository path, as the
@vercel/nestjswrapper does (it returnsjoin(dir, entrypointFromOutputDir), so the handler stays atdist/main.jsbeside the service'spackage.jsonandnode_modules), - or accept a service
entrypointthat the build command creates.
Workaround
"entrypoint": "src/main.ts" together with "outputDirectory": "." makes the builder compile the source instead of reusing dist. The "." behaviour is from #16761. Without ".", a dist left in the tree is picked up again.
Reproduction
- Create a pnpm workspace with
apps/api: a NestJS app with"type": "module",src/main.tscallingapp.listen(), and"build": "nest build". - Add the
vercel.jsonabove at the repository root, and set the project framework to Services. - Run
vercel build. Then open.vercel/output/services/api/functions/index.func/.vc-config.json:handlerismain.jsandfilePathMapmaps it toapps/api/dist/main.js. Deploy, and request any path.
Source: vercel/vercel