The process.env not defined after deploying to ubuntu 18.04 - node.js

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

Related

Error: connect ECONNREFUSED 127.0.0.1:6379 at TCPConnectWrap.afterConnect [as oncomplete]

I have run the server and getting an error as:
events.js:292
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
Emitted 'error' event on Queue instance at:
at RedisClient.<anonymous> (C:\Users\DELL\Documents\Vs Code Folders\Feelare\node_modules\kue\lib\redis.js:65:13)
at RedisClient.emit (events.js:315:20)
at RedisClient.on_error (C:\Users\DELL\Documents\Vs Code Folders\Feelare\node_modules\redis\index.js:401:14)
at Socket.<anonymous> (C:\Users\DELL\Documents\Vs Code Folders\Feelare\node_modules\redis\index.js:279:14)
at Socket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
My Code is as:
const env = require('./environment');
mongoose.connect(`mongodb://localhost/${env.db}`);
const db = mongoose.connection;
db.on('error', console.error.bind(console, "Error connecting to MongoDB"));
db.once('open', function(){
console.log('Connected to Database :: MongoDB');
});
module.exports = db;
I have tried by installing redis and adding it into script but error is same.And also tried in production and development environment but the error is same.
This issue seems to be with redis server. Try running redis-server in the terminal.
Hope this helps!

ECONNREFUSED , I am trying to connect to my prosgres database running the app with nodemon

node:internal/process/promises:246
triggerUncaughtException(err, true /* fromPromise */);
^
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}

Can't connect to redis bull from google cloud run

I'm trying to setup my redis bull on Google Cloud run. Locally everything works.
I use the following code when the server starts:
const client = redis.createClient('6379', '10.103.YYY.YYY');
This seems to be working. I don't receive any error on startup.
When I try to launch a job with Bull like:
const actionQueue = new Bull(uuid(), {
redis: {
port: 6379, host: '10.103.YYY.YYY', tls: {
servername: '10.103.YYY.YYY'
}
}
});
await actionQueue.add();
actionQueue.process(async (job) => {
return this._job();
});
actionQueue.on('completed', async (job, actionId) => {
console.log(`Job completed with result ${actionId}`);
});
actionQueue.on("failed", (job, error) => {
console.log(job.id, error);
});
But the redis is still connecting to localhost ip&port. Anyone an idea why this is still connecting to my localhost? Do I need to setup on different way?
Error log:
2020-11-21T14:55:35.794783Z <rejected> Error: connect ECONNREFUSED 127.0.0.1:6379
Standaard
2020-11-21T14:55:35.794791Z at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16) {
Standaard
2020-11-21T14:55:35.794798Z errno: 'ECONNREFUSED',
Standaard
2020-11-21T14:55:35.794804Z code: 'ECONNREFUSED',
Standaard
2020-11-21T14:55:35.794810Z syscall: 'connect',
Standaard
2020-11-21T14:55:35.794816Z address: '127.0.0.1',
Standaard
2020-11-21T14:55:35.794822Z port: 6379
Standaard
2020-11-21T14:55:35.794828Z }
Standaard
2020-11-21T14:55:35.794834Z}
Standaard
2020-11-21T14:55:35.794987Z The error was: Error: connect ECONNREFUSED 127.0.0.1:6379
Standaard
2020-11-21T14:55:35.794994Z at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16) {
Standaard
2020-11-21T14:55:35.795Z errno: 'ECONNREFUSED',
Standaard
2020-11-21T14:55:35.795006Z code: 'ECONNREFUSED',
Standaard
2020-11-21T14:55:35.795011Z syscall: 'connect',
Standaard
2020-11-21T14:55:35.795017Z address: '127.0.0.1',
Standaard
2020-11-21T14:55:35.795022Z port: 6379
Standaard
2020-11-21T14:55:35.795028Z}
Cloud Run doesn't speak to the VPC natively. You need to bridge Serverless world with out project VPC.
For this, you have to use serverless VPC connector. Create on in the same region as your Cloud Run is deployed.
Then attach it to your Cloud Run service. Like this, the private range (at least, you can also route all the outgoing traffic) traffic is routed to the connector and lands on your VPC. Now you can access to private resources in your VPC like your redis instance.

How can I connect redis running on my machine from my app running in a docker container

My nodejs app is running in a docker container. And I'm using bull queue for the background services which uses redis.
I have redis installed on my machine and when I try to run the queue in pm2 I'm unable to connect to redis
Here's the config for redis in my app
"redis" : {
"host": "192.168.1.98",
"port": "6379"
}
Here's the error
0|stockProcessor | WARNING: NODE_APP_INSTANCE value of '0' did not match any instance config file names.
0|stockProcessor | WARNING: See https://github.com/lorenwest/node-config/wiki/Strict-Mode
0|stockProcessor | Error: connect ECONNREFUSED 127.0.0.1:6379
0|stockProcessor | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1129:14) {
0|stockProcessor | errno: 'ECONNREFUSED',
0|stockProcessor | code: 'ECONNREFUSED',
0|stockProcessor | syscall: 'connect',
0|stockProcessor | address: '127.0.0.1',
0|stockProcessor | port: 6379
0|stockProcessor | }
0|stockProcessor | Error: connect ECONNREFUSED 127.0.0.1:6379
0|stockProcessor | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1129:14) {
0|stockProcessor | errno: 'ECONNREFUSED',
0|stockProcessor | code: 'ECONNREFUSED',
0|stockProcessor | syscall: 'connect',
0|stockProcessor | address: '127.0.0.1',
0|stockProcessor | port: 6379
0|stockProcessor | }
Config injection
const Queue = require("bull");
const configR = require("config");
const redisConfig = configR.get("redis");
const constants = require("../constants");
const config = {
settings: {
stalledInterval: 5000,
maxStalledCount: 9999,
lockDuration: 60000,
lockRenewTime: 30000
}
};
const stockQueue = new Queue("stockQueue", redisConfig, config);
Appreciate any help.
Thanks in advance
If you check the documentation it looks like the second parameter in the constructor should be a string instead of an object. It looks like you could add your redis config option to the config object to connect to it, or build a string from your redis config object.
P.S. I've not used bull before, so it might be that the documentation is incorrect here and your way of passing properties is allowed

Connecting to postgres on Heroku with Node.js

I try to connect to my postgres database on Heroku by:
var pg = require('pg');
pg.defaults.ssl = true;
var pool = new pg.Pool(process.env.DATABASE_URL);
pool.connect(function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
I have confirmed the value of proccess.env.DATABASE_URL it has the right format, and I can successfully connect to it via psql postgres://XXXXXXXX:XXXXXXXXXXXXXXX#XXXXXXXXXXXXX.eu-west-1.compute.amazonaws.com:5432/XXXXXXXX
This is the output, and what confuses me is the 127.0.0.1 reference if this output.
2016-06-27T14:58:52.714154+00:00 app[web.1]: Express server listening on port 18119
2016-06-27T14:59:29.175122+00:00 app[web.1]: error fetching client from pool { Error: connect ECONNREFUSED 127.0.0.1:5432
2016-06-27T14:59:29.175133+00:00 app[web.1]: at Object.exports._errnoException (util.js:949:11)
2016-06-27T14:59:29.175134+00:00 app[web.1]: at exports._exceptionWithHostPort (util.js:972:20)
2016-06-27T14:59:29.175135+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
2016-06-27T14:59:29.175136+00:00 app[web.1]: code: 'ECONNREFUSED',
2016-06-27T14:59:29.175137+00:00 app[web.1]: errno: 'ECONNREFUSED',
2016-06-27T14:59:29.175137+00:00 app[web.1]: syscall: 'connect',
2016-06-27T14:59:29.175138+00:00 app[web.1]: address: '127.0.0.1',
2016-06-27T14:59:29.175139+00:00 app[web.1]: port: 5432 }
Try this:
var pg = require('pg').native
pg.connect(process.env.databaseURL, function (err, conn, done) {
...
}

Resources