简体   繁体   English

PM2和DotEnv的问题在ubuntu服务器上不起作用

[英]Issue with PM2 and DotEnv not working on ubuntu server

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. 我知道这个问题有答案,但是我不想再创建一个配置文件并在其中加载所有配置并运行pm2进程。

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

Inside 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"
  }
}

As you see from my package.json file that I am loading the node -r dotenv/config index.js file from scripts > start 从您的package.json文件中可以看到,我正在从脚本>开始加载node -r dotenv / config index.js文件

When I run locally with the following command 当我使用以下命令在本地运行时

npm start npm开始

The project works totally fine. 该项目完全正常。

Now I have deployed the project to the server and there if I manually run 现在我已经将项目部署到服务器上,如果我手动运行

npm start npm开始

then works fine. 然后工作正常。

When I install PM2 in Ubuntu Server in production and do the following steps then it's not working. 当我在生产环境中的Ubuntu Server中安装PM2并执行以下步骤时,它将无法正常工作。

Step 1: Mode inside the project folder in the root directory and 步骤1:在根目录下的项目文件夹中进行模式设置,然后

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

Then getting the following 然后得到以下

 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. 有什么问题。

Even when I run the following as 即使当我运行以下内容时

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

Then getting error as 然后得到错误

error: unknown option `-r' 错误:未知选项`-r'

Any other solution to run the script with pm2 使用pm2运行脚本的任何其他解决方案

You need to follow the notes from my answer here: https://stackoverflow.com/a/55853036/2208713 . 您需要在这里遵循我的回答中的注释: https : //stackoverflow.com/a/55853036/2208713 I can see from your question above you are mixing up pm2 syntax with npm. 我可以从上面的问题中看到,您正在将pm2语法与npm混合使用。 If you take the pattern from my answer you will be able to get this working easily enough - but follow my instructions carefully! 如果您采用我的回答中的模式,则可以轻松完成此操作-但请仔细遵循我的指示!

There are 2 ways to achieve a solution. 有两种解决方案。

Solution 1: 解决方案1:

When running the pm2 process run with the --node-args as follows 当运行pm2进程时,使用--node-args如下运行

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

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 您可以传递多个用空格隔开的参数,除了dotenv / config之外,我并不需要太多,因为我正在从dotenv包中加载所有内容,但仅显示了以下示例

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

Solution 2: 解决方案2:

Alternatively, you can initialize your project with pm2 init this will create the pm2 configuration file with name ecosystem.config.js 或者,您可以使用pm2 init初始化您的项目,这将创建名称为生态系统 .config.js的pm2配置文件。

For me for some reasons the args under app didn't work so I had to add node_args again as follows 对我来说,由于某些原因, 应用程序下的args无法正常工作,因此我不得不再次添加node_args ,如下所示

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

Actually, I am sticking with solution 1 for cleaner and minimal code mode. 实际上,我坚持使用解决方案1以获得更简洁和最少的代码模式。

In case if anyone interested with PM2 options then please visit the following link 如果有人对PM2选项感兴趣,请访问以下链接

http://pm2.keymetrics.io/docs/usage/quick-start/ http://pm2.keymetrics.io/docs/usage/quick-start/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM