• features
  • ESM

ESM 支持

¥ESM Support

从 v8.15.x 开始,node-postgres 支持 ECMAScript 模块 (ESM) 格式。这意味着你可以使用 import 语句代替 requireimport pg from 'pg'

¥As of v8.15.x node-postgres supporters the ECMAScript Module (ESM) format. This means you can use import statements instead of require or import pg from 'pg'.

仍然支持 CommonJS 模块。ESM 格式是一项可选功能,不会影响使用 CommonJS 的现有代码库。

¥CommonJS modules are still supported. The ESM format is an opt-in feature and will not affect existing codebases that use CommonJS.

文档已更改为展示 ESM 的使用方法,但在 CommonJS 环境中,你仍然可以使用相同的代码,只需更改导入格式即可。

¥The docs have been changed to show ESM usage, but in a CommonJS context you can still use the same code, you just need to change the import format.

如果你使用的是 CommonJS,可以使用以下代码导入 pg 模块:

¥If you're using CommonJS, you can use the following code to import the pg module:

  const pg = require('pg')
  const { Client } = pg
  // etc...

ESM 使用

¥ESM Usage

如果你使用的是 ESM,可以使用以下代码导入 pg 模块:

¥If you're using ESM, you can use the following code to import the pg module:

  import { Client } from 'pg'
  // etc...

以前,如果你使用 ESM,则必须使用以下代码:

¥Previously if you were using ESM you would have to use the following code:

  import pg from 'pg'
  const { Client } = pg
  // etc...
Last updated on April 25, 2025