Cloud Function Cannot Connect To PostgreSQL With SequelizeJS In Production (Works Locally)

This code snippet works perfectly locally with netlify dev:

const { Sequelize } = require('sequelize');
const sequelize = new Sequelize(process.env.DATABASE_URI); // postgres uri
sequelize.create({...});

However, when I deploy this to production, the function logs this error message:

{"errorType":"Error","errorMessage":"Please install pg package manually","stack":["Error: Please install pg package manually","    at ConnectionManager._loadDialectModule (/var/task/node_modules/sequelize/dist/lib/dialects/abstract/connection-manager.js:55:15)","...

I confirmed that all the node packages are installed before site deployment, including pg. Did anyone also encounter issue?

I have to add this line

require('pg')

on the top of the file for my production deployment to work. Not sure why this discrepancy exists between production and dev.

Hello!
I can connect to remote postgresql database in heroku when I run the code locally by using “netlify dev” but it doesn not work when I deploy it in netlify.

I tried to use “import pg from “pg”;” I got this solution from node.js - “Error: Please install pg package manually” when trying to run “npm run serve:dev”? - Stack Overflow

But it still does not work for me when I install and publish it in netlify.
“import pg from “pg”;”
import { Sequelize, DataTypes } from “sequelize”;

const sequelize = new Sequelize(process.env.DB_URI, {
dialect: “postgres”,
dialectModule: pg,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
},
},
});
<…>

When I use it from the “hello.js” netlify function, the error in netlify log is:
Jan 18, 10:39:00 PM: 876873b5 ERROR Error is: TypeError: import_Colors.default.findOne is not a function
at Runtime.handler (/var/task/netlify/functions/hello.js:37:47)
at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)

it seems like it does not recognize findOne when the app deployed in the netlify site, but again locally the same code works just fine.

It seems like it is “sequelize” ORM issue only because I can run SQL statements without any problems when the code is deployed in the netlify site.

Any thoughts why it is happening and how to fix it?

@shavkatsh Are you aware that heroku and Netlify are not equivalent environments?

See:

I am NOT trying to run any database or server in Netlify.

My database is hosted by Heroku. Heroku provides the url that I use to connect to the postgresql database in Heroku remotely.
My little test web app can be hosted by either my local machine OR by Netlify site, it is static JS/React code, it is not a server.
When I run this code locally on my machine I am able to connect to the Heroku database using “sequelize” without any problem using “netlify dev” and without it. That means that Heroku database can be connected from anywhere.
But when I deploy the exactly same test app and run it in netlify site it cannot use “sequelize” with remote Heroky database.
Why?

I’ve got no idea.

Sorry I’d misread your your initial sentence and was just ensuring your weren’t trying to transfer a project from heroku over to Netlify unchanged.

Netlify’s functions are AWS lambda’s so you could read this (if you haven’t already):

No problem!
I haven’t seen this article, I should read it and try what they suggest. Thank you for this link!