如何在(之前)PM2 启动时要求 dotenv/config 文件
How to require dotenv/config file on (before) PM2 starts
我运行这样的节点应用程序:
node -r dotenv/config dist/app
我需要使用 PM2 的类似东西:
pm2 start -r dotenv/config dist/app.js --name appname // doesn't work
我收到下一个错误:error: unknown option -r
我制作了一个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
命令也是如此 - 每个帐户都应该这样做。
希望对您有所帮助。
使用 node_args.
pm2 start --node-args="-r dotenv/config" dist/app.js --name appname
None 这对我有用,因为我使用的是生态系统文件和集群模式,它的行为非常奇怪(不像没有集群模式...)。
我在根目录安装了 dotenv 作为开发依赖项(我也在使用纱线工作区)。
然后我这样做了:
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,
}
]
}
我运行这样的节点应用程序:
node -r dotenv/config dist/app
我需要使用 PM2 的类似东西:
pm2 start -r dotenv/config dist/app.js --name appname // doesn't work
我收到下一个错误:error: unknown option -r
我制作了一个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
命令也是如此 - 每个帐户都应该这样做。
希望对您有所帮助。
使用 node_args.
pm2 start --node-args="-r dotenv/config" dist/app.js --name appname
None 这对我有用,因为我使用的是生态系统文件和集群模式,它的行为非常奇怪(不像没有集群模式...)。
我在根目录安装了 dotenv 作为开发依赖项(我也在使用纱线工作区)。
然后我这样做了:
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,
}
]
}