如何一起做 Import 和 Require?

How to do Import and Require together?

节点 v14,服务器后端需要 Observable 并连接到 PostgreSql。

index.js

import { Observable } from 'rxjs';
const observable = new Observable(
  subscriber => 
  {
    ...
  });

observable.subscribe({...});
console.log('just after subscribe');

要连接到 PostgreSql

const pgp = require('pg-promise')();
const db = pgp({...});

现在 ReferenceError: require is not defined.

我怎样才能两者兼得?

经过大量研究和尝试,回到同一个地方,只需将 require 替换为 import。以上面为例,

原文:

const pgp = require('pg-promise')();
const db = pgp({...});

新:

import pgPromise from 'pg-promise';
const pgp = pgPromise({...});
const db = pgp({...});