From eb8a9f2c4f5c68e7cfdcaafe9fcf63cbd98c40a3 Mon Sep 17 00:00:00 2001 From: Connor Pearson Date: Thu, 31 Oct 2024 15:51:44 +0100 Subject: [PATCH] fix(vercel): clamp runtime config to valid node versions (#2819) --- src/presets/vercel/utils.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/presets/vercel/utils.ts b/src/presets/vercel/utils.ts index 275a9ae4ce..a15c6cd2f0 100644 --- a/src/presets/vercel/utils.ts +++ b/src/presets/vercel/utils.ts @@ -10,13 +10,28 @@ import type { VercelServerlessFunctionConfig, } from "./types"; +// https://vercel.com/docs/functions/runtimes/node-js/node-js-versions +const SUPPORTED_NODE_VERSIONS = [16, 18, 20, 22]; + +function getSystemNodeVersion() { + const systemNodeVersion = Number.parseInt( + process.versions.node.split(".")[0] + ); + + return Number.isNaN(systemNodeVersion) ? 20 : systemNodeVersion; +} + export async function generateFunctionFiles(nitro: Nitro) { const buildConfigPath = resolve(nitro.options.output.dir, "config.json"); const buildConfig = generateBuildConfig(nitro); await writeFile(buildConfigPath, JSON.stringify(buildConfig, null, 2)); - const systemNodeVersion = process.versions.node.split(".")[0]; - const runtimeVersion = `nodejs${systemNodeVersion}.x`; + const systemNodeVersion = getSystemNodeVersion(); + const usedNodeVersion = + SUPPORTED_NODE_VERSIONS.find((version) => version >= systemNodeVersion) ?? + SUPPORTED_NODE_VERSIONS.at(-1); + + const runtimeVersion = `nodejs${usedNodeVersion}.x`; const functionConfigPath = resolve( nitro.options.output.serverDir, ".vc-config.json"