Jhonsore
Jhonsore

Reputation: 1857

Error Please install pg package manually with AWS Lambda serverless framework

recently i had a big headache deploying my serveless application.

I have a lambda function that connect to a Postgresql database. To deploy my application i use serverless framework.

So, after doing my code when testing my code i got the follow error:

Please install pg package manually

But, i have installed pg on my package.json.

All the same the error still happened.

So, why do i have this error if the pg is installed correctly?

Upvotes: 1

Views: 3099

Answers (1)

Jhonsore
Jhonsore

Reputation: 1857

After hours and hours of searching and changing my code, i decided to refactor it starting from the bottom.

I took off all plugins in serverless, put the raw code and everything worked fine.

With this i had a direction. Some of my serverless plugins was breaking my connection with Postgresql.

So, after testing i got that the break was happening because of serverless-bundle plugin that i use to optimize my app.

After searching, i got the answer on the package website.

To use the Sequelize package along with pg, you'll need to ignore it from Webpack and using the dialectModule option. Read more here.

In your serverless.yml:

custom:
  bundle:
    ignorePackages:
      - pg-native

And in your Lambda code:

const sequelize = new Sequelize(
  process.env.DB_NAME,
  process.env.DB_USERNAME,
  process.env.DB_PASSWORD,
  {
    host: process.env.DB_HOST,
    dialect: process.env.DB_DIALECT,
    dialectModule: pg
  }
);

So, that's it!

Upvotes: 4

Related Questions