Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 856
Matias Salimbene Avatar asked Oct 18 '18 14:10

Matias Salimbene


2 Answers

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.

like image 72
Joel Stevick Avatar answered Oct 30 '22 01:10

Joel Stevick


For without constructor use .default

const PouchDB = require('pouchdb').default;
like image 28
ßãlãjî Avatar answered Oct 30 '22 00:10

ßãlãjî