kysely-org/kysely-postgres-js

TypeError: Cannot read properties of undefined (reading 'replace')

Closed this issue ยท 13 comments

Same as with this issue: #9

I'm using latest of everything and this is on nextjs 14, node 20.

I tried to understand the problem but I failed to do so.
I rolled back to pg and worked fine, therefore I think it's either postgres or this adapter.

image

Hey ๐Ÿ‘‹

Can you paste your package.json's dependencies object or provide a reproduction repository?

Hi @igalklebanov , sorry but I already migrated out of nextjs.
I think MAYBE the problem is that the connection string was undefined. Not completely sure, but that might be.
Sorry.

Hey, @igalklebanov

I have the same error as op

D:\backend\node_modules\postgres\cjs\src\connection.js:389
      stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug },
                                               ^
TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (D:\backend\node_modules\postgres\cjs\src\connection.js:389:48)
    at errored (D:\backend\node_modules\postgres\cjs\src\connection.js:384:17)
    at Socket.error (D:\backend\node_modules\postgres\cjs\src\connection.js:376:5)
    at Socket.emit (node:events:514:28)
    at Socket.emit (node:domain:489:12)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)

package.json

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "npx tsc",
    "start": "node dist/index.js",
    "dev": "nodemon src/index.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^16.4.2",
    "express": "^4.18.2",
    "kysely": "^0.27.2",
    "kysely-postgres-js": "^2.0.0",
    "pg": "^8.11.3"
  },
  "devDependencies": {
    "@types/express": "^4.17.21",
    "@types/node": "^20.11.17",
    "kysely-codegen": "^0.11.0",
    "nodemon": "^3.0.3",
    "ts-node": "^10.9.2",
    "typescript": "^5.3.3"
  }
}

Exactly same issue, while default pg dialect works as should.

"kysely-postgres-js": "^2.0.0",
"pg": "^8.11.3"

Hey ๐Ÿ‘‹

This has pg in it, instead of postgres as postgres client library.
Try npm i postgres@3.4.1.

same problem next 12.3.4 or next 14

I got the same problem as well, here's the error it gives

file:///app/node_modules/postgres/src/connection.js:389
      stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug },
                                               ^

TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///app/node_modules/postgres/src/connection.js:389:48)
    at errored (file:///app/node_modules/postgres/src/connection.js:383:14)
    at TLSSocket.data (file:///app/node_modules/postgres/src/connection.js:318:9)
    at TLSSocket.emit (node:events:518:28)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
    at Readable.push (node:internal/streams/readable:390:5)
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23)

Node.js v20.11.1
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Here's the deps version I'm using

"kysely": "^0.27.2",
"kysely-postgres-js": "^2.0.0",
"postgres": "^3.4.3",

This project is purely express running on Node 20 on docker container in a kubernetes cluster.

This seems related to postgres, and not this package as all stack traces thus far lead to https://github.com/porsager/postgres/blob/master/src/connection.js#L389

Try using your postgres instance outside of Kysely with the same options - does it still throw an exception?

Closing this.

Tests are passing with Node LTS, postgres@latest, kysely@latest. https://github.com/kysely-org/kysely-postgres-js/actions/runs/8107957535

Was able to reproduce the same exception when docker container was down or by passing a wrong host option. Seems to happen because a connection cannot be established - could be due to many reasons.. the connection options are missing/wrong at runtime, your database instance refuses connections from your network OR your database instance requires something additional, SSL perhaps?

Not related to Kysely or kysely-postgres-js.

I guess it unrelated to this adapter but host is valid because it works for some time and than nextjs hot reload crashes it and after server restart it works again

I can confirm that this is related to a malformed URL. In my case I had a ?schema= in my connection string and postgres-js didn't like it. cc @porsager, it's a cryptic error message and might be worth exploring a fix.

I can confirm that this is related to a malformed URL. In my case I had a ?schema= in my connection string and postgres-js didn't like it. cc @porsager, it's a cryptic error message and might be worth exploring a fix.

Yeah maybe but i use credentials not url and error still happens

I can confirm that this is related to a malformed URL. In my case I had a ?schema= in my connection string and postgres-js didn't like it. cc @porsager, it's a cryptic error message and might be worth exploring a fix.

I remove ?schema= from url and it work