首页 > 解决方案 > 无法从 node-pg 导入客户端

问题描述

我正在尝试在 Node 项目中使用 Postgresql。我正在使用模块化导入,因此在导入“pg”时遇到问题:

import * as pg from 'pg'
const { Client } = pg
let client = new Client()

导致这个错误

        let client = new Client()
                     ^
TypeError: Client is not a constructor

我已经查看了与此类似的其他几个问题,但仍有问题:

import { native as pg } from 'pg';
let client = new pg.Client()

导致此错误:

import { native as pg } from 'pg';
         ^^^^^^
    SyntaxError: Named export 'native' not found. The requested module 'pg' is a CommonJS module, which may not support all module.exports as named exports.

有谁知道我可以尝试正确进行此导入吗?

标签: node.jspostgresql

解决方案


从错误建议来看,pg 是一个 CommonModule,它可能不支持所有 module.exports 作为命名导出。

更改导入

import * as pg from 'pg'

import pg from 'pg'

将解决导入问题。


推荐阅读