【问题标题】:How to require dotenv/config file on (before) PM2 starts如何在 PM2 启动之前要求 dotenv/config 文件
【发布时间】:2019-11-11 21:40:14
【问题描述】:

我像这样运行节点应用程序:

node -r dotenv/config dist/app

我需要使用 PM2 类似的东西:

pm2 start -r dotenv/config dist/app.js --name appname // doesn't work

我收到下一个错误:error: unknown option -r

【问题讨论】:

标签: node.js pm2


【解决方案1】:

我已经做了一个Shell脚本:

// pm2-start.sh

NODE_ENV=production &&
node -r dotenv/config dist/app

然后我跑了pm2 start pm2-start.sh --name appname

提示我也运行了:pm2 startup 然后复制了 pm2 指示运行的命令,以激活通过 pm2 注册的所有应用程序的自动启动。

然后我运行pm2 save 来保存自动服务。

注意:pm2 分别在服务器帐户之间明确列出应用程序。这意味着在用户 A 上列出的应用不会在用户 B 上列出。pm2 startup 命令也是如此 - 每个帐户都应该这样做。

希望对你有帮助。

【讨论】:

    【解决方案2】:

    使用node_args

    pm2 start --node-args="-r dotenv/config" dist/app.js --name appname
    

    【讨论】:

      【解决方案3】:

      这些都不适合我,因为我使用的是生态系统文件和集群模式,它的行为非常奇怪(不像没有集群模式......)。

      我在根目录安装了 dotenv 作为 dev 依赖项(我也在使用 yarn 工作区)。

      然后我这样做了:

      require('dotenv').config({ path: 'path/to/your/.env' })
      
      module.exports = {
          apps: [
              {
                  name: 'app',
                  script: 'server/dist/index.js',
                  instances: 2,
                  exec_mode: 'cluster',
                  instance_var: 'APP_INSTANCE_SEQ',
                  // listen_timeout: 10000,
                  // restart_delay: 10000,
              }
          ]
      }
      
      

      【讨论】:

        猜你喜欢
        • 2020-01-10
        • 2019-09-07
        • 2020-09-22
        • 1970-01-01
        • 2020-12-16
        • 1970-01-01
        • 1970-01-01
        • 2020-01-31
        • 2018-12-16
        相关资源
        最近更新 更多