Error requiring lib on serverless

I’m using lib “pg” to connect to a PostgreSQL instance.
It works running local, but not in production:

Pool is not a constructor

manifest.json

{
  "platform-version": "2.0",
  "product": {
    "freshdesk": {
      "location": {
        "ticket_sidebar": {
          "url": "ticket_sidebar.html",
          "icon": "styles/images/icon.svg"
        },
        "full_page_app": {
          "url": "full_page_app.html",
          "icon": "styles/images/icon.svg"
        }
      }
    }
  },
  "dependencies": {
    "postman-request": "2.88.1-postman.28",
    "btoa": "1.2.1",
    "xml2js": "0.4.23",
    "util": "0.12.3",
    "async": "3.2.0",
    "pg": "8.6.0",
    "url": "0.11.0",
    "tough-cookie": "4.0.0"
  },
  "whitelisted-domains": [
    "https://*.freshdesk.com",
  ]
}

server/lib/queue.js

const { Pool } = require("pg");
var pg;

class Queue {
  constructor(host, port, user, password, database) {
    pg = new Pool({
      user: user,
      host: host,
      database: database,
      password: password,
      port: port,
    });
  }
  
  async testConnection() {
    try {
      await pg.query("SELECT NOW() as now");
    } catch (error) {
      throw new Error("> Queue > testConnection() - " + error.message);
    }
  }
}

exports.class = Queue;

server/server.js:

const Queue = require("./lib/queue");
var queue;

exports = {
  events: [
    { event: "onAppInstall", callback: "onAppInstallCallback" },
    { event: "onTicketCreate", callback: "onTicketCreateHandler" },
    { event: "onTicketUpdate", callback: "onTicketUpdateHandler" },
    { event: "onConversationCreate", callback: "onConversationCreateHandler" },
    { event: "onExternalEvent", callback: "onExternalEventHandler" },
  ],
  
  ...
  
  pgAuthenticate: function (options) {
    queue = new Queue.class(options.pgHost, options.pgPort, options.pgUser, options.pgPassword, options.pgDatabase);
    queue
      .testConnection()
      .then(() => {
        renderData(undefined, undefined);
      })
      .catch((error) => {
        console.error(error);
        renderData(error);
      });
  },
};  

config/assets/iparams.js

  ...
  async function pgAuthenticate(options) {
    return new Promise((resolve, reject) => {
      client.request.invoke("pgAuthenticate", options).then(
        (data) => {
          resolve(data.response);
        },
        (error) => {
          reject(error);
        }
      );
    });
  }
  ...

The “pgAuthenticate” method is called when clicked in a button on the installation page.
I described here the issue with “pg”, but the same is happening with another lib I’m using: “xml2js”.

This is very strange.
I didn’t mentioned before, but it was working with:
const { Pool } = require("pg");
in production as well and suddenly stopped.
I didn’t mentioned before because I wasn’t sure.

But now I changed to:

const PG = require("pg");
...
pg = new PG.Pool({})

Then I published in the production account and it worked for a while.
I even redraw the topic here.
But now It’s happening again:

PG.Pool is not a constructor

It stopped working again!

Are you referring to node-postgres/package.json at v7.14.0 · brianc/node-postgres · GitHub? If so, I am seeing the latest version at 7.14 and not 8.6 as mentioned in dependencies in manifest.json. Can you please set it at 7.14 and try again?

1 Like

Hey @ManiDeepak_Vandrangi
Yes, it is this lib.
I got the version 8.6 from here: pg - npm
But I changed to 7.14 as you said, still getting the same error.
As I said, it’s very strange, because it worked for a while.

can you please try locally with sample nodeJS snippet? It should work in same way in serverless app as well unless it uses some local environment variables.

What do you mean by sample nodeJS snippet?
If it’s just to test it locally, as I said before, it’s working.
Never had a problem running locally.
This only happens when I install the app in the account.
As I said, after install in the account, it worked for a while, then it stopped.

can you please privately share the account’s domain and the app name? I will check it from ourside

Ok, sent in private.

1 Like

Hey guys…
I’m still having this issue.
I tried to use the PG Client instead Pool, but the same still happen.
When I call

const { Client } = require("pg");
var pg = new Client();

I get:

Client is not a constructor

I noticed that this always happens after I update the app.

A little more context of this app:
The onTicketCreate and onTicketUpdate events when fired, will connect to PG and make a insert.
And I have a scheduler that runs every 1 minute that makes a select in the PG database and process the data as needed.

Would it be the app update process that someway is breaking the lambda function while the connection with PG is active?