部署Nuxt项目到Vercel时报FUNCTION_INVOCATION_FAILED错误如何解决?
问题描述
我从GitHub导入了一个Nuxt项目,配置的vercel.json如下:
{ "version": 2, "builds": [ { "src": "nuxt.config.js", "use": "@nuxtjs/vercel-builder" } ] }
我的package.json配置如下:
{ "name": "test-app-v2", "version": "1.0.0", "private": true, "scripts": { "dev": "nuxt", "build": "nuxt build", "start": "nuxt start", "generate": "nuxt generate", "lint:js": "eslint --ext \".js,.vue\" --ignore-path .gitignore .", "lint": "yarn lint:js" }, "lint-staged": { "*.{js,vue}": "eslint" }, "husky": { "hooks": { "pre-commit": "lint-staged" } }, "dependencies": { "@nuxtjs/composition-api": "^0.24.0", "@nuxtjs/pwa": "^3.3.5", "core-js": "^3.9.1", "nuxt": "^2.15.3" }, "devDependencies": { "@nuxt/types": "^2.15.3", "@nuxt/typescript-build": "^2.1.0", "@nuxtjs/eslint-config-typescript": "^6.0.0", "@nuxtjs/eslint-module": "^3.0.2", "@nuxtjs/vuetify": "^1.11.3", "babel-eslint": "^10.1.0", "eslint": "^7.22.0", "eslint-config-prettier": "^8.1.0", "eslint-plugin-nuxt": "^2.0.0", "eslint-plugin-prettier": "^3.3.1", "eslint-plugin-vue": "^7.7.0", "husky": "^4.3.8", "lint-staged": "^10.5.4", "prettier": "^2.2.1" } }
nuxt.config.js配置如下:
import colors from 'vuetify/es5/util/colors' export default { // Global page headers: https://go.nuxtjs.dev/config-head head: { titleTemplate: '%s - test-app-v2', title: 'test-app-v2', meta: [ { charset: 'utf-8' }, { name: 'viewport', content: 'width=device-width, initial-scale=1' }, { hid: 'description', name: 'description', content: '' }, ], link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }], }, // Global CSS: https://go.nuxtjs.dev/config-css css: [], // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins plugins: [], // Auto import components: https://go.nuxtjs.dev/config-components components: true, // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules buildModules: [ // https://go.nuxtjs.dev/typescript '@nuxt/typescript-build', // https://go.nuxtjs.dev/vuetify '@nuxtjs/vuetify', '@nuxtjs/composition-api/module', ], // Modules: https://go.nuxtjs.dev/config-modules modules: [ // https://go.nuxtjs.dev/pwa '@nuxtjs/pwa', ], // PWA module configuration: https://go.nuxtjs.dev/pwa pwa: { manifest: { lang: 'en', }, }, // Vuetify module configuration: https://go.nuxtjs.dev/config-vuetify vuetify: { customVariables: ['~/assets/variables.scss'], theme: { dark: false, themes: { dark: { primary: colors.blue.darken2, accent: colors.grey.darken3, secondary: colors.amber.darken3, info: colors.teal.lighten1, warning: colors.amber.base, error: colors.deepOrange.accent4, success: colors.green.accent3, }, }, }, }, // Build Configuration: https://go.nuxtjs.dev/config-build build: {}, }
目前项目还没有开发任何API,Vercel控制台函数标签页中的报错日志如下:
请问该如何排查并解决这个FUNCTION_INVOCATION_FAILED错误?
排查与解决步骤
- 调整运行环境版本
Nuxt 2.15.x仅兼容Node.js 14/16版本,Vercel当前默认Node版本为18及以上,版本不兼容会导致SSR函数启动失败。可以在Vercel控制台进入项目设置→函数面板,将Node.js版本修改为16.x,也可以在项目根目录新增.nvmrc文件,写入内容16指定版本。 - 补全并调整依赖位置
你的package.json中缺少vuetify核心依赖,@nuxtjs/vuetify需要vuetify作为对等依赖才能正常运行,同时@nuxtjs/vuetify属于运行时需要的依赖,不应放在devDependencies下。执行以下命令调整即可:
yarn add vuetify @nuxtjs/vuetify
- 优化vercel.json配置
@nuxtjs/vercel-builder已经停止维护,Vercel现已原生支持Nuxt 2项目部署,不需要额外指定builder。可以直接删除项目根目录的vercel.json文件,Vercel会自动识别Nuxt项目并匹配默认配置。如果要保留配置,修改为以下内容即可:
{ "version": 2, "buildCommand": "nuxt build", "devCommand": "nuxt", "installCommand": "yarn install" }
- (可选)切换为静态部署
如果不需要SSR功能,可以直接修改Vercel构建命令为yarn generate,输出目录设置为dist,静态部署不需要调用SSR函数,从根源避免函数调用报错。
内容的提问来源于stack exchange,提问作者wobsoriano
相关产品推荐
相关产品推荐

