node-postgres: TypeError: Cannot read properties of undefined (reading 'prepareValue')

Hi, Latest PR: https://github.com/brianc/node-postgres/commit/940479bc4b601a89a6fc915be959ca96e83dd4d0 broke our runtime completely. This occurred even though we did not upgrade the version of pg so it was a complete surprise.

Issue is coming from: https://github.com/brianc/node-postgres/blob/940479bc4b601a89a6fc915be959ca96e83dd4d0/packages/pg-cursor/index.js#L4

TypeError: Cannot read properties of undefined (reading 'prepareValue')
    at Object.<anonymous> (/<path>/node_modules/pg-cursor/index.js:4:23)
    at Module._compile (node:internal/modules/cjs/loader:1565:14)
    at node:internal/modules/cjs/loader:1708:10
    at Object.require.extensions.<computed> [as .js] (/<path>/node_modules/ts-node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:1318:32)
    at Function._load (node:internal/modules/cjs/loader:1128:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:219:24)
    at Module.require (node:internal/modules/cjs/loader:1340:12)
    at require (node:internal/modules/helpers:138:16)

Please look into it as soon as possible. pg version: 8.14.1 node version: 22.12.0 npm version: 10.9.0

Let me know if you need any other details.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 11
  • Comments: 19 (9 by maintainers)

Most upvoted comments

I think I understand it now.

the #2353 PR made changes to the pg-cursor package, specifically this: https://github.com/brianc/node-postgres/blob/940479bc4b601a89a6fc915be959ca96e83dd4d0/packages/pg-cursor/index.js#L2-L4

it was like this before: https://github.com/brianc/node-postgres/blob/81ec0635fc1836c99b5975a8225ce6ede901e7d5/packages/pg-cursor/index.js#L2-L3

and pg-cursor package (a few other packages too actually) got this peer dependency: https://github.com/brianc/node-postgres/blob/940479bc4b601a89a6fc915be959ca96e83dd4d0/packages/pg-cursor/package.json#L30-L32

That’s why all previous versions broke, because from minor 8.15.x, the path of import/require changed, and with the peer dependency fixed at 8^, pg-cursor will always come with the latest minor/patch, which no longer supports that form of import.

EDIT:

Ok, I’ve been thinking for a while, and I’ve decided not to open a PR since it ultimately depends on what @brianc wants to do, there are a few possible ways to fix it.

For either of the options below (or even another one), it might also be a good idea to start pinning major and minor versions in peer dependencies across packages, to avoid issues in future releases.

First option

Apply a workaround at
https://github.com/brianc/node-postgres/blob/940479bc4b601a89a6fc915be959ca96e83dd4d0/packages/pg-cursor/index.js#L2-L4

Modify it like this so both older and newer versions of pg work:

const pg = require('pg')
const Result = pg.Result || require('pg/lib/result.js')
const prepare = (pg.utils && pg.utils.prepareValue) || require('pg/lib/utils.js').prepareValue

Then deprecate this approach in a future major release.

Second option

Revert all ESM-related changes and release a new 8.15.6 or 8.16.0 version, then reintroduce the ESM changes in a new 9.0.0.
This way, older packages such as pg-cursor depending on "pg": "^8" won’t break unexpectedly.

Third option

Advise users to apply the override workaround when using packages that depend on older pg internals.

fixed in pg-cursor@2.14.6