javascriptnode.jselectronpouchdb

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

  • 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.