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

Remix框架Cloudflare Pages服务端环境变量访问问题求助

解决Remix部署Cloudflare Pages时环境变量"process is not defined"问题

Cloudflare Pages的边缘函数运行在V8 isolates环境,不支持Node.js的process对象,因此不能用process.env获取环境变量。针对Remix的Cloudflare适配环境,需要改用import.meta.env来访问变量,同时配合正确的配置。

解决步骤

1. 修改session.server.ts代码

将所有process.env替换为import.meta.env,修改后的代码如下:

import { createCookieSessionStorage } from "@remix-run/cloudflare";

export const { getSession, commitSession, destroySession } = createCookieSessionStorage({
  cookie: {
    name: "remix_session",
    secrets: [import.meta.env.SESSION_SECRET],
    sameSite: "lax",
    path: "/",
    secure: import.meta.env.NODE_ENV === "production",
    httpOnly: true,
    maxAge: 60 * 60 * 24 * 30, // 30 days
  },
});

2. 配置remix.config.js

确保remix.config.js设置为ESM模块格式,并显式声明需要暴露的环境变量:

/** @type {import('@remix-run/cloudflare').CloudflareRemixConfig} */
export default {
  serverModuleFormat: "esm",
  env: {
    SESSION_SECRET: true,
    NODE_ENV: true
  },
};

3. 配置Cloudflare Pages环境变量

在Cloudflare Pages控制台的项目Settings > Environment Variables中,添加SESSION_SECRET变量并设置对应的值。

本地开发注意事项

  • 在项目根目录创建.env文件,添加SESSION_SECRET=你的密钥内容
  • 将.env添加到.gitignore中,避免敏感信息泄露
  • 启动本地开发服务器后,Remix会自动加载.env中的变量到import.meta.env

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 02:33:27