imabnormal
imabnormal16mo ago

Resolving Class constructor error

Hello, I am currently trying to port a small test api from NodeJS to Deno. This code uses only NPM packages and is a direct copy past from the previous project besides the "npm:" addition to packages. Currently when running the code, there are no errors on compile, only during run time when trying to create the postgress client, which seems to come from the postgress library. I have not been succesful in finding a solution either here in discord or online. Only semi relevant would be changing the compile target which I also tried but got the following error
Unsupported compiler options in "file:///C:/Users/Abnormal/Documents/Projects/Web/notesapp/denoback/deno.json".
The following options were ignored:
target
Unsupported compiler options in "file:///C:/Users/Abnormal/Documents/Projects/Web/notesapp/denoback/deno.json".
The following options were ignored:
target
This was using the example compiler options from https://deno.land/manual@v1.36.4/advanced/typescript/configuration
{
"compilerOptions": {
"target": "esnext",
"lib": ["dom", "dom.iterable", "dom.asynciterable", "deno.ns"]
}
}
{
"compilerOptions": {
"target": "esnext",
"lib": ["dom", "dom.iterable", "dom.asynciterable", "deno.ns"]
}
}
Attached is the code as in the main.ts file, along with the error produced when calling the function. Thank you
import postgres from 'npm:postgres';
const client = postgres({
user: 'postgres',
database: 'test',
hostname: 'localhost',
port: 5433,
password: 'example'
});
import postgres from 'npm:postgres';
const client = postgres({
user: 'postgres',
database: 'test',
hostname: 'localhost',
port: 5433,
password: 'example'
});
Error:
error: Uncaught (in promise) TypeError: Class constructor Socket cannot be invoked without 'new'
at createSocket (file:///C:/Users/Abnormal/AppData/Local/deno/npm/registry.npmjs.org/postgres/3.3.5/src/connection.js:131:15)
at Timeout.connect [as _onTimeout] (file:///C:/Users/Abnormal/AppData/Local/deno/npm/registry.npmjs.org/postgres/3.3.5/src/connection.js:328:31)
at cb (ext:deno_node/internal/timers.mjs:63:31)
at Object.action (ext:deno_web/02_timers.js:153:11)
at handleTimerMacrotask (ext:deno_web/02_timers.js:67:10)
at eventLoopTick (ext:core/01_core.js:189:21)
error: Uncaught (in promise) TypeError: Class constructor Socket cannot be invoked without 'new'
at createSocket (file:///C:/Users/Abnormal/AppData/Local/deno/npm/registry.npmjs.org/postgres/3.3.5/src/connection.js:131:15)
at Timeout.connect [as _onTimeout] (file:///C:/Users/Abnormal/AppData/Local/deno/npm/registry.npmjs.org/postgres/3.3.5/src/connection.js:328:31)
at cb (ext:deno_node/internal/timers.mjs:63:31)
at Object.action (ext:deno_web/02_timers.js:153:11)
at handleTimerMacrotask (ext:deno_web/02_timers.js:67:10)
at eventLoopTick (ext:core/01_core.js:189:21)
Edit: While there is a deno port of drizzle it is no longer up to date. And the Postgres deno library is not compatible with the npm drizzle library.
Deno
Configuration | Manual | Deno
TypeScript comes with a lot of different options that can be configured, but Deno strives to make it easy to use TypeScript with Deno. Lots of different options frustrates that goal. To make things e
2 Replies
Deno AI Helper
Deno AI Helper16mo ago
Based on the documentation, it seems like you're trying to use an npm package directly in Deno, which might not work as expected due to differences in the runtime environments of Node.js and Deno. Deno has its own way of handling database connections, and it provides a Postgres client that you can use. Here's an example of how to connect to a Postgres database using Deno:
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";

const client = new Client({
user: "user",
database: "test",
hostname: "localhost",
port: 5432,
});
await client.connect();
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";

const client = new Client({
user: "user",
database: "test",
hostname: "localhost",
port: 5432,
});
await client.connect();
You can then execute SQL queries like this:
const result = await client.queryArray("SELECT ID, NAME FROM PEOPLE");
console.log(result.rows); // [[1, 'Carlos'], [2, 'John'], ...]
const result = await client.queryArray("SELECT ID, NAME FROM PEOPLE");
console.log(result.rows); // [[1, 'Carlos'], [2, 'John'], ...]
And don't forget to close the connection when you're done:
await client.end();
await client.end();
To run this example, you would use the Deno CLI like this:
deno run --allow-net --allow-env https://examples.deno.land/postgres.ts
deno run --allow-net --allow-env https://examples.deno.land/postgres.ts
Please note that the --allow-net and --allow-env flags are necessary to allow network access and access to environment variables, respectively. As for the TypeScript configuration, Deno has its own TypeScript runtime and does not use tsconfig.json. The target and lib options are set by Deno and cannot be changed. If you need to use a specific TypeScript configuration for your project, you might need to compile your TypeScript code to JavaScript first, and then run the JavaScript code with Deno. If you're still having issues, it would be helpful to know more about the specific error message you're encountering. The error message you provided seems to suggest that there's an issue with the way the Socket class is being used, but without more context, it's hard

Did you find this page helpful?