Matias Salimbene
Matias Salimbene

Reputation: 605

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!

Upvotes: 8

Views: 2596

Answers (4)

Achraf Lahcen
Achraf Lahcen

Reputation: 41

change:

var PouchDB = require('pouchdb');

to:

import PouchDB from 'pouchdb'

Upvotes: 3

siddharth verma
siddharth verma

Reputation: 40

This error is coming because the compiler is not able to get the pouch DB constructor from the package.

To use pouch DB as a function & without the constructor:-

  • Import pouch DB as :- const PouchDB = require('pouchdb').default; in your TS file.
  • Then run: npm i --save-dev @types/node command to install the type support for node from the npm registry.
  • Then in tsconfig.app.json file add "types": ["node"] in compiler object. This step is used to support node types example 'require'.
  • To instantiate the database add this in your TS file const db = PouchDB('MyDB');
  • Then serve the project

To use pouch DB with the constructor:-

  • Import pouch DB as:- import PouchDB from 'pouchdb' in your TS file.
  • To instantiate the database add this to your TS file const db = new PouchDB('MyDb');
  • Then serve the project.

Upvotes: 1

Joel Stevick
Joel Stevick

Reputation: 2028

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.

Upvotes: 6

Balaji
Balaji

Reputation: 10877

For without constructor use .default

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

Upvotes: 4

Related Questions