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

如何用PM2运行带ORIGIN参数的SvelteKit Node.js应用?

解决SvelteKit应用PM2配置ORIGIN环境变量的问题

下面给你三种可行的方案,按需选择:

方案一:启动命令直接指定环境变量

直接在PM2启动命令中传入ORIGIN参数:

# 方式1:命令前缀指定
ORIGIN=http://myorigin pm2 start out/index.js --name "sveltekit-app"

# 方式2:用--env参数传递
pm2 start out/index.js --name "sveltekit-app" --env ORIGIN=http://myorigin

方案二:使用PM2生态配置文件(推荐)

创建ecosystem.config.js文件,把环境变量写入配置,方便统一管理:

module.exports = {
  apps: [
    {
      name: 'sveltekit-app',
      script: 'out/index.js',
      env: {
        ORIGIN: 'http://myorigin'
      }
    }
  ]
};

执行启动命令:
pm2 start ecosystem.config.js

方案三:结合dotenv使用

如果想用.env文件管理环境变量,需要让PM2启动时加载dotenv模块,两种实现方式:

命令行方式

pm2 start --interpreter node --interpreter-args "-r dotenv/config" out/index.js --name "sveltekit-app"

配置文件方式

修改ecosystem.config.js,添加interpreter_args配置:

module.exports = {
  apps: [
    {
      name: 'sveltekit-app',
      script: 'out/index.js',
      interpreter_args: '-r dotenv/config'
    }
  ]
};

启动后PM2会自动加载项目根目录下.env文件里的ORIGIN变量,确保已安装dotenv依赖(npm install dotenv --save)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 02:55:18