简体   繁体   中英

The process.env not defined after deploying to ubuntu 18.04

I deployed my nodejs 10.16.3 app to ubuntu 18.04 from my win10 PC development. The problem is that the process.env becomes undefined on ubuntu server. The dotenv module is used as:

require('dotenv').config({path: process.cwd() +'/config/.env'});

The server is listening to:

const port = process.env.PORT; // 3000;
console.log(process.env);

server.listen(port, () => {
  console.log(`env var: ${process.env.jwtPrivateKey}`)
  console.log(`Listening on port ${port}...`);

});

There is a .env file under myproj\config\ storing all the user defined params. Her is a portion of the file:

PORT = 3000
DB_PASSWORD = mydbpassword
jwtPrivateKey = myprivatekey
jwt_token_expire_days = 24
jwt_secret_len = 10
vcode_time_elapse = 10

After starting the nodejs app with:

pm2 start /ebs/www/myapp/index.js

Here is printout from index-out.log :

$cat index-out.log
env var: undefined
Listening on port undefined...

Here is the index-error.log :

$ cat index-error.log
Unable to connect to the database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:5433
    at connection.connect.err (/ebs/www/emps/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:170:24)
    at Connection.connectingErrorHandler (/ebs/www/emps/node_modules/pg/lib/client.js:174:14)
    at Connection.emit (events.js:198:13)
    at Socket.reportStreamError (/ebs/www/emps/node_modules/pg/lib/connection.js:72:10)
    at Socket.emit (events.js:198:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
  name: 'SequelizeConnectionRefusedError',
  parent:
   { Error: connect ECONNREFUSED 127.0.0.1:5433
       at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
     errno: 'ECONNREFUSED',
     code: 'ECONNREFUSED',
     syscall: 'connect',
     address: '127.0.0.1',
     port: 5433 },
  original:
   { Error: connect ECONNREFUSED 127.0.0.1:5433
       at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
     errno: 'ECONNREFUSED',
     code: 'ECONNREFUSED',
     syscall: 'connect',
     address: '127.0.0.1',
     port: 5433 } }

It must be something related to configuration of dotenv module.

Change your require dotenv statement from

require('dotenv').config({path: process.cwd() +'/config/.env'});

to

require('dotenv').config({path: __dirname +'/config/.env'});

Since the project is started from different location using below command, it is not able to read the.env file:

pm2 start /ebs/www/myapp/index.js

Difference between process.cwd() and __dirname:

process.cwd() returns the current working directory, the directory from which you invoked the node command.

__dirname returns the directory name of the directory containing the JavaScript source code file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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