我正在尝试以下操作:
const { Client } = require('pg');
console.log(Client);
const client = new Client({
user: 'Username censored',
host: 'Host censored',
database: 'gisuebung',
password: 'Passworded censored',
port: 5432,
});
client.connect();
但是,当我运行此程序时,我收到以下错误:Error in v-on handler: "TypeError: Client is not a constructor"
我在网上找到一个片段后写了这篇文章,似乎无论我在哪里看,人们都做了完全相同的事情。有人能告诉我我做错了什么吗?
3条答案
按热度按时间chhkpiq41#
这是一个JS错误:
试试这个,它对我很有效:
const { Client } = require('pg');
const Client = require('pg').Client;
-- ES模块:
import pg from 'pg';
const Client = pg.Client;
anauzrmj2#
您的代码对于CommonJS是正确的。但是对于ESM,将引发此错误。
在ESM中运行的正确方法:
wribegjk3#
试试这个,它对我很有效: