PM2和DotEnv的问题在ubuntu服务器上不起作用 [英] Issue with PM2 and DotEnv not working on ubuntu server

查看:300
本文介绍了PM2和DotEnv的问题在ubuntu服务器上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有这个问题的答案,但是我不想再创建一个配置文件并在那里加载所有配置并运行pm2进程.

I know that there are answers for this question, but I don't want to create one more config file and load all the configuration there and run the pm2 process.

Project Structure
-----------------
.env
index.js -> server is listening in this file
routes/
models/
middleware/
startup/
package.json
...

内部 package.json

{
  "name": "eventbooking",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node -r dotenv/config index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@hapi/joi": "^15.0.3",
    "bcryptjs": "^2.4.3",
    "compression": "^1.7.4",
    "dotenv": "^8.0.0",
    "express": "^4.17.1",
    "express-async-errors": "^3.1.1",
    "helmet": "^3.18.0",
    "joi-objectid": "^2.0.0",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.5.14",
    "winston": "^3.2.1"
  }
}

从我的 package.json 文件中可以看到,我正在从 scripts>开始加载 node -r dotenv/config index.js 文件

As you see from my package.json file that I am loading the node -r dotenv/config index.js file from scripts > start

当我使用以下命令在本地运行时

When I run locally with the following command

npm开始

该项目完全正常.

现在我已经将项目部署到服务器上,如果我手动运行

Now I have deployed the project to the server and there if I manually run

npm开始

然后工作正常.

当我在生产环境中的Ubuntu Server中安装 PM2 并执行以下步骤时,它将无法正常工作.

When I install PM2 in Ubuntu Server in production and do the following steps then it's not working.

第1步:在根目录下的项目文件夹内进行模式

Step 1: Mode inside the project folder in the root directory and

pm2 start index.js --name "Event Booking"

然后得到以下内容

 App name │ id │ version │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem      │ user │ watching │
├──────────┼────┼─────────┼──────┼───────┼────────┼─────────┼────────┼─────┼──────────┼──────┼──────────┤ 

│ index    │ 0  │ 1.0.0   │ fork │ 29897 │ online │ 0       │ 0s     │ 0%  │ 3.7 MB   │ root │ disabled 

但是该项目无法正常工作.有什么问题.

But the project not working. Whats the issue.

即使我以以下方式运行

pm2 start -r dotenv/config index.js --name 'Event Booking'

然后出现错误

错误:未知选项`-r'

任何其他可在pm2上运行脚本的解决方案

Any other solution to run the script with pm2

推荐答案

有两种方法可以实现解决方案.

There are 2 ways to achieve a solution.

解决方案1:

在运行pm2进程时,使用-node-args 进行如下操作

When running the pm2 process run with the --node-args as follows

pm2 start index.js --name eventbooking --node-args="-r dotenv/config"

除了 dotenv/config 之外,您还可以传递多个空格分隔的参数,因为我从 dotenv 包中加载了所有内容,但我不需要太多,但仅用于演示如下

You can pass multiple arguments with space separated, other than dotenv/config I didn't need much as I am loading everything from dotenv package but showing just for demo as follows

pm2 start index.js --name eventbooking --node-args="-r dotenv/config --production --port=1337"

解决方案2:

或者,您可以使用 pm2 init 初始化项目,这将创建名称为 ecosystem.config.js

Alternatively, you can initialize your project with pm2 init this will create the pm2 configuration file with name ecosystem.config.js

由于某些原因,应用下的参数不起作用,所以我不得不再次添加 node_args ,如下所示

For me for some reasons the args under app didn't work so I had to add node_args again as follows

{
  "apps": [
    {
      "name": "eventbooking",
      "script": "./index.js",
      "node_args": ["-r dotenv/config"]
    }
  ]
}

实际上,我坚持使用解决方案1 ​​以获得更简洁和最少的代码模式.

Actually, I am sticking with solution 1 for cleaner and minimal code mode.

如果有人对PM2选项感兴趣,请访问以下链接

In case if anyone interested with PM2 options then please visit the following link

http://pm2.keymetrics.io/docs/usage/quick-开始/

这篇关于PM2和DotEnv的问题在ubuntu服务器上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆