Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 部署到ubuntu 18.04后未定义process.env_Node.js_Ubuntu_Dotenv - Fatal编程技术网

Node.js 部署到ubuntu 18.04后未定义process.env

Node.js 部署到ubuntu 18.04后未定义process.env,node.js,ubuntu,dotenv,Node.js,Ubuntu,Dotenv,我将我的nodejs10.16.3应用程序从我的win10 PC开发版部署到ubuntu 18.04。问题是在ubuntu服务器上,process.env变成了undefined。dotenv模块用作: require('dotenv').config({path: process.cwd() +'/config/.env'}); 服务器正在侦听: const port = process.env.PORT; // 3000; console.log(process.env); server

我将我的nodejs10.16.3应用程序从我的win10 PC开发版部署到ubuntu 18.04。问题是在ubuntu服务器上,
process.env
变成了
undefined
dotenv
模块用作:

require('dotenv').config({path: process.cwd() +'/config/.env'});
服务器正在侦听:

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}...`);

});
myproj\config\
下有一个
.env
文件存储所有用户定义的参数。她是文件的一部分:

PORT = 3000
DB_PASSWORD = mydbpassword
jwtPrivateKey = myprivatekey
jwt_token_expire_days = 24
jwt_secret_len = 10
vcode_time_elapse = 10
启动nodejs应用程序后,请执行以下操作:

pm2 start /ebs/www/myapp/index.js
这是从
索引输出.log
打印出来的:

$cat index-out.log
env var: undefined
Listening on port undefined...
$ 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 } }
以下是索引错误。日志:

$cat index-out.log
env var: undefined
Listening on port undefined...
$ 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 } }

它必须与
dotenv
模块的配置有关

将require dotenv语句从

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

由于使用以下命令从不同位置启动项目,因此无法读取.env文件:

pm2 start /ebs/www/myapp/index.js
process.cwd()和_dirname之间的差异:

返回当前工作目录,即调用
节点
命令的目录


返回包含JavaScript源代码文件的目录的目录名

dotenv如果在您声明的路径上找不到该文件,则返回错误。还有一个调试选项,在这种情况下也很有用。