node.jspostgresqlexpressnpm

NodeJS and Express: "Error: self signed certificate"


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. :)


Solution

  • 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.