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

如何部署RemixJS独立版?实现NextJS同款打包效果

RemixJS 实现类似 NextJS output: "standalone" 的独立部署方案

RemixJS 没有 NextJS 中直接的 output: "standalone" 配置项,但可以通过以下两种主流方式实现无需服务器端执行 npm install 的独立部署:

方式一:用 esbuild 打包服务器代码与依赖

这是最常用的方案,能将服务器代码连同所有依赖打包成单个文件:

  1. 创建打包配置文件(比如项目根目录下的 build-server.js):
const esbuild = require("esbuild");

esbuild.build({
  entryPoints: ["./server.js"], // 替换为你的实际服务器入口文件路径
  bundle: true,
  platform: "node",
  target: "node18", // 匹配你的生产环境 Node.js 版本
  outfile: "./build/server.bundle.js",
  // 若有不需要打包的原生模块(如特定数据库驱动),可添加到 external 数组
  external: []
}).catch(() => process.exit(1));
  1. 修改 package.json 的脚本命令:
{
  "scripts": {
    "build": "remix build && node build-server.js",
    "start": "node build/server.bundle.js"
  }
}
  1. 执行 npm run build 后,会生成两个关键目录/文件:
    • build/server.bundle.js:包含所有依赖的完整服务器代码
    • public/build:客户端静态资源
      部署时只需将这两个目录/文件上传到服务器,直接运行 node build/server.bundle.js 即可启动服务,无需执行 npm install。

方式二:用 pkg 打包成跨平台可执行文件

如果需要生成无需 Node.js 环境即可运行的单个可执行文件,可使用 pkg 工具:

  1. 安装依赖:
npm install -D pkg
  1. 修改 package.json,指定入口并添加打包脚本:
{
  "main": "./server.js", // 替换为你的实际服务器入口文件路径
  "scripts": {
    "build": "remix build",
    "pkg": "pkg . --targets node18-linux-x64,node18-win-x64,node18-macos-x64 --output dist/remix-app"
  }
}
  1. 执行 npm run build && npm run pkg,会在 dist 目录生成对应平台的可执行文件。部署时只需将可执行文件和 public/build 目录上传到服务器,直接运行可执行文件即可启动服务,无需服务器安装 Node.js 或任何依赖。

注意事项

  • 若项目使用环境变量,需确保部署时正确配置,或在打包阶段注入。
  • 对于动态加载的模块或特殊原生依赖,需根据工具文档调整 external(esbuild)或 assets(pkg)配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 04:35:05