Uncaught TypeError: PouchDB is not a constructor
Asked Answered
G

4

8

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!

Gouty answered 18/10, 2018 at 14:6 Comment(2)
this is probably electron specific, it works fine in vanilla node.jsEke
There's a number of possible fixes detailed at github.com/pouchdb/pouchdb/issues/6692Pantin
G
6

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.

Grivet answered 31/7, 2020 at 18:45 Comment(0)
D
4

For without constructor use .default

const PouchDB = require('pouchdb').default;
Douche answered 11/6, 2020 at 6:9 Comment(0)
S
2

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

to : import PouchDB from 'pouchdb'

Smoothbore answered 16/3, 2022 at 19:6 Comment(0)
D
1

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.
Downing answered 20/7, 2022 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.