Reputation: 605
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
Reputation: 41
change:
var PouchDB = require('pouchdb');
to:
import PouchDB from 'pouchdb'
Upvotes: 3
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:-
const PouchDB = require('pouchdb').default;
in your TS file.npm i --save-dev @types/node
command to install the type support for node from the npm registry."types": ["node"]
in compiler object. This step is used to support node types example 'require'.const db = PouchDB('MyDB');
To use pouch DB with the constructor:-
import PouchDB from 'pouchdb'
in your TS file.const db = new PouchDB('MyDb');
Upvotes: 1
Reputation: 2028
I found the solution here
To use PouchDB with Typescript:
Install PouchDB
Add to your source file
At the top in your "imports" section
const PouchDB = require('pouchdb-browser');
const pouchDB = PouchDB.default.defaults();
To instantiate the database
const db = new pouchDB('MyDB');
The rest is all here.
Upvotes: 6
Reputation: 10877
For without constructor use .default
const PouchDB = require('pouchdb').default;
Upvotes: 4