You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Vercel部署的Next.js项目中获取locales目录路径

在Vercel部署的Next.js项目中获取Fluent locales目录的正确路径

问题原因

用process.cwd() + '/src/locales'失效是因为Vercel部署时,Next.js的构建产物会被放到/var/task目录,但src目录不会被完整复制到该路径,导致找不到指定目录。

解决方案

1. CommonJS模块场景(使用require语法)

直接利用__dirname获取当前文件所在的src/目录,再拼接相对路径定位到locales:

const path = require('path');
const localesDir = path.resolve(__dirname, './locales');

2. ES模块场景(使用import语法)

通过import.meta.url获取当前文件的URL,再转换为文件路径来定位:

import { fileURLToPath } from 'url';
import path from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const localesDir = path.resolve(__dirname, './locales');

3. 确保Vercel构建时包含locales文件

在项目根目录创建vercel.json,添加配置让Vercel打包服务端函数时包含locales目录下的所有.ftl文件:

{
  "functions": {
    "src/**/*.ts": {
      "includeFiles": "src/locales/**/*.ftl"
    }
  }
}

内容的提问来源于stack exchange,提问作者Alexiuscrow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 20:49:53