6

I'm a beginner in NodeJS, and I have a very simple Node/Express application that uses PostGreSQL as the database. My "db.js" file looks like this:

const { Pool } = require('pg');

module.exports = new Pool({
    user: 'postgres',
    password: 'xxxx',
    host: 'localhost',
    port: 5432,
    database:'gymmanager'
});

When I was only running locally, everything was working fine, so I decided do deploy the app to Heroku. In order to do so, I've created a .env file with my environment variables for development. Here is the .env file:

NODE_ENV=dev
DB_USER='postgres'
DB_PASS='xxxx'
DB_HOST='localhost'
DB_PORT=5432
DB_DATABASE='gymmanager'

And I have changed my "db.js" file, that now looks like this:

if(process.env.NODE_ENV !== 'dev') {

    const { Client } = require('pg');

    const client = new Client({  
        connectionString: process.env.DATABASE_URL,
        ssl: true
    });

    client.connect();
    module.exports = client;

} else {

    require('dotenv').config();
    const { Pool } = require('pg');

    module.exports = new Pool({
        user: process.env.DB_USER,
        password: process.env.DB_PASS,
        host: process.env.DB_HOST,
        port: process.env.DB_PORT,
        database: process.env.DB_DATABASE,
        rejectUnauthorized: false
    });    
}

My app was deployed and is running smoothly on Heroku, but now, when I run 'npm start' locally, I get the following Error:

(node:12989) UnhandledPromiseRejectionWarning: Error: self signed certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1491:34)
    at TLSSocket.emit (events.js:315:20)
    at TLSSocket._finishInit (_tls_wrap.js:933:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:691:12)

I have tried several solutions...

Ignore invalid self-signed ssl certificate in node.js with https.request?

nodejs - error self signed certificate in certificate chain

But none of them seems to work. Can anyone help me?

Thanks in advance. :)

Caroline
  • 373
  • 1
  • 5
  • 13

1 Answers1

7

Can you try, export NODE_TLS_REJECT_UNAUTHORIZED=0 on the command line? This sets the property globally rather than process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; which set the property to that particular process. Hope you also executed npm config set strict-ssl false.

  • I have tried these commands, and now I'm getting a different error. It says "error: password authentication failed", and I found out its because Node is not reading the ```NODE_ENV=dev``` variable in the .env file.. When I run ```NODE_ENV=dev npm start``` works! Do you have any ideia why it is not getting the variable from the .env file? All the other env variables are working fine. – Caroline May 09 '20 at 04:02
  • Make sure to require the dotenv package at the top, since you've declared the ```NODE_ENV``` property inside your ```.env``` file. You can also try out packages like config(https://www.npmjs.com/package/config) to separate dev and prod environment configs. – Karthick Ramachandran May 09 '20 at 06:11
  • Thanks for the reply! Everything is working fine. But if I require the dotenv package at the top, I'll be requiring the package on Heroku as well, because I'm using the same "db.js" file for both applications. Since I'm not importing the dotend package on Herou, wouldn't that be a problem ? – Caroline May 09 '20 at 17:22
  • I would suggest requiring the package at the top and setting the ```NODE_ENV``` property to ```PROD``` for heroku environment. You can easily set env variables for heroku. Please go through that here https://devcenter.heroku.com/articles/config-vars#using-the-heroku-dashboard – Karthick Ramachandran May 10 '20 at 03:07