3

When I run a jest test, creating a Pool instance when I require the pool, it returns a _pg.Pool is not a constructor error.

I have tried looking at the StackOverflow: pg.Pool is not a constructor

And this still does not work.

However, I am able to create a pool instance when I run the code, the error only shows up in Jest.

Node code:

import { Pool } from 'pg'

const pool = new Pool({configs})

export default pool

Error log:

● Test suite failed to run

    TypeError: _pg.Pool is not a constructor

    > 6 | const pool = new Pool({
        |              

      at Object.<anonymous> (src/resources/connection.js:6:14)
      at Object.require (src/routes/api.js:2:20)
      at Object.<anonymous> (src/__tests__/integration/user.test.js:8:1)

sidenote: the code is a copy of the documentation in https://node-postgres.com/api/pool

I don't expect an error to occur, since pg.Pool is a class with a constructor.

2
  • I am having the same problem. Did you manage to fix it, @L. Ars? Commented Mar 5, 2020 at 16:41
  • @heniotierra I didn't manage to fix it back then, and I'm no longer working on that code, but thanks for your help! Commented Mar 6, 2020 at 17:23

2 Answers 2

2

In case anyone comes across the same problem, I solved it by installing pg-pool, which has been merged into main pg package, and then importing pg-pool's Pool instead of pg's.

Reference: https://github.com/brianc/node-postgres/tree/master/packages/pg-pool

Sign up to request clarification or add additional context in comments.

Comments

2

I wanted to add an alternate answer because for me the issue was very subtle and simple to fix.

I have a wrapper module that leverages pg and which historically has imported it via:

import * as postgresql from 'pg';

I've always used this.pool = new postgresql.Pool( { ...bits } );

This of course led to the following error today when updating my module to be pure ESM:

TypeError: postgresql.Pool is not a constructor
    at new PostgreSQLDriver (file:///home/rik/workspaces/kwaeri/node-kit/postgresql-database-driver/dist/src/postgresql-database-driver.mjs:35:278)
    at file:///home/rik/workspaces/kwaeri/node-kit/postgresql-database-driver/dist/test/postgresql-database-driver.mjs:23:559
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:528:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:438:15)
    at async formattedImport (/home/rik/workspaces/kwaeri/node-kit/postgresql-database-driver/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at async exports.loadFilesAsync (/home/rik/workspaces/kwaeri/node-kit/postgresql-database-driver/node_modules/mocha/lib/nodejs/esm-utils.js:91:20)
    at async singleRun (/home/rik/workspaces/kwaeri/node-kit/postgresql-database-driver/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async exports.handler (/home/rik/workspaces/kwaeri/node-kit/postgresql-database-driver/node_modules/mocha/lib/cli/run.js:370:5)

I tried using named exports as the documentation seems to suggest in the Check, use, return section, but that got me:

file:///home/rik/workspaces/kwaeri/node-kit/postgresql-database-driver/dist/src/postgresql-database-driver.mjs:23
cov_1u3o9s5ali=function(){return actualCoverage;};}return actualCoverage;}cov_1u3o9s5ali();import{Pool}from'pg';import{DatabaseDriver}from'@kwaeri/database-driver';// You'll need this line and the method definitions below
                                                                                                  ^^^^
SyntaxError: Named export 'Pool' not found. The requested module 'pg' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'pg';
const {return actualCoverage;};}return actualCoverage;}cov_1u3o9s5ali();import{Pool}from'pg';import{DatabaseDriver} = pkg;

... As I had actually expected it would.

Anyways, the fix is hinted to by typescript in the syntax error above - doh:

import Postgres from `pg`;

// ...
this.pool = new Postgres.Pool( { ...bits } );

That resolves the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.