process.env 部署到 ubuntu 18.04 后未定义
The process.env not defined after deploying to ubuntu 18.04
我将我的 nodejs 10.16.3 应用程序从我的 win10 PC 开发部署到 ubuntu 18.04。问题是 process.env
在 ubuntu 服务器上变为 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
这是 index-out.log
的打印输出:
$cat index-out.log
env var: undefined
Listening on port undefined...
这里是 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 } }
一定是跟dotenv
模块的配置有关。
更改
中的 require dotenv 语句
require('dotenv').config({path: process.cwd() +'/config/.env'});
到
require('dotenv').config({path: __dirname +'/config/.env'});
由于项目是使用以下命令从不同位置启动的,因此无法读取 .env 文件:
pm2 start /ebs/www/myapp/index.js
process.cwd() 和 __dirname 的区别:
process.cwd()
returns 当前工作目录,您从中调用 node
命令的目录。
__dirname
returns 包含 JavaScript 源代码文件的目录的目录名称
我将我的 nodejs 10.16.3 应用程序从我的 win10 PC 开发部署到 ubuntu 18.04。问题是 process.env
在 ubuntu 服务器上变为 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
这是 index-out.log
的打印输出:
$cat index-out.log
env var: undefined
Listening on port undefined...
这里是 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 } }
一定是跟dotenv
模块的配置有关。
更改
中的 require dotenv 语句require('dotenv').config({path: process.cwd() +'/config/.env'});
到
require('dotenv').config({path: __dirname +'/config/.env'});
由于项目是使用以下命令从不同位置启动的,因此无法读取 .env 文件:
pm2 start /ebs/www/myapp/index.js
process.cwd() 和 __dirname 的区别:
process.cwd()
returns 当前工作目录,您从中调用 node
命令的目录。
__dirname
returns 包含 JavaScript 源代码文件的目录的目录名称