'Uncaught TypeError: PouchDB is not a constructor

I'm struggling to understand this error: Uncaught TypeError: PouchDB is not a constructor

The code is as follows:

var PouchDB = require("pouchdb");
var db = new PouchDB("scr");

I've read about how it may be related to types and that adding:

 "@types/node": "^10.12.0",
 "@types/pouchdb": "^6.3.2",

to my package.json should help, but it isn't. I've tested on another simple .js file and works, but on my main app it isn't. Still, I don't understand why it wouldn't work. The pouch documentation is quite clear https://pouchdb.com/api.html#create_document.

I should mention I'm running this on the context of an electron app, not in the browser.

I'm baffled at this point, any help would be greatly appreciated. Cheers!



Solution 1:[1]

I found the solution here

To use PouchDB with Typescript:

Install PouchDB

  1. npm install pouchdb-browser
  2. npm install --save-dev @types/pouchdb-browser

Add to your source file

  1. At the top in your "imports" section

    const PouchDB = require('pouchdb-browser');

    const pouchDB = PouchDB.default.defaults();

  2. To instantiate the database

    const db = new pouchDB('MyDB');

The rest is all here.

Solution 2:[2]

For without constructor use .default

const PouchDB = require('pouchdb').default;

Solution 3:[3]

change : var PouchDB = require('pouchdb');

to : import PouchDB from 'pouchdb'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joel Stevick
Solution 2 ßãlãjî
Solution 3 Achraf Lahcen