【问题标题】:is there a way to import { pool } from pg有没有办法从 pg 导入 { pool }
【发布时间】:2022-01-15 06:26:57
【问题描述】:

我正在尝试导入 Pool,但出现错误。

这是被抛出的错误:

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

这是我的代码:

import { Pool } from "pg";

dotenv.config();

const databaseConfig = { connectionString: process.env.DATABASE_URL };
const pool = new Pool(databaseConfig);

export default pool;

【问题讨论】:

    标签: javascript node.js postgresql ecmascript-6


    【解决方案1】:

    您应该使用默认导出:

    import pg from "pg";
    const { Pool } = pg;
    dotenv.config();
    
    const databaseConfig = { connectionString: process.env.DATABASE_URL }; const pool = new Pool(databaseConfig);
    
    export default pool;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-23
      • 2019-03-26
      • 2018-01-25
      • 1970-01-01
      • 2016-03-16
      • 2014-10-13
      • 1970-01-01
      • 2021-10-03
      相关资源
      最近更新 更多