How do I use pouchdb with typescript?
Asked Answered
T

5

8

I am trying to use pouchdb with Typescript. I cannot link to the pouchdb module.

import { PouchDB } from "pouchdb"

reports that it cannot find module pouchdb, even though it is in node_modules.

I also cannot find the appropriate typings for pouchdb.

Transship answered 3/6, 2016 at 9:58 Comment(0)
T
0

I managed to get the module recognised by using

declare function require(a)

var PouchDB = require("pouchdb")

I have given up type checking, but at least I can make progress.

Transship answered 3/6, 2016 at 13:46 Comment(1)
Have you had any luck? i have the same issue, it's kinda working but need to reload page on new browser for data to appear. @LeRoy i've tried your method with no luck. Lot's of people having the same issue. I'm using angular2.Pizarro
N
4

I'm doing this in Ionic, so I may be missing a step on getting the types file loaded properly.

Make sure your types are installed with:

npm install --save-dev @types/pouchdb

At the top of your data service import pouch like so:

import * as PouchDB from 'pouchdb';

* edit *

I don't have all the facts, but this is my current understanding. Typings is no longer needed in Typescript >2.0 I believe typescript now works automatically with types files installed from DefinitelyTyped. DefinitelyTyped is an official central repository that is kept current like npm. And even if I'm dead wrong about all this, DefinitelyTyped is still better than typings and has a much bigger community.

Nation answered 12/2, 2017 at 3:1 Comment(1)
This has helped me, when no other methods did using TypeScript and Angular2.Overissue
C
3

Cause i just had this Problem, For Angular 2 + Typescript the correct way to use PouchDB (using angular-cli) is to:

  1. ng new SOMENAME
  2. npm install --save pouchdb
  3. npm install --save-dev @types/pouchdb
  4. In your app.component import PouchDB from 'pouchdb';
  5. In your App Component Class public db: any; and to init this.db = new PouchDB('test'); // , {storage:'persistent'} not working in typescript without updating typings

see https://github.com/nolanlawson/pouchdb-find/issues/201.

If you have problems installing the packages on windows with an EPERM Error use (f.e.) npm install --save pouchdb --no-optional to disable the warning. The installation should still be ok. For more info see https://github.com/npm/npm/issues/17671

Crimmer answered 6/9, 2017 at 7:58 Comment(0)
T
3

I had the same problem trying to import into Angular 6.

Your imports seem fine:

import PouchDB from 'pouchdb';
import PouchFind from 'pouchdb-find';
PouchDB.plugin(PouchFind);

What you may be missing is you need to add this to your polyfills.ts file:

(window as any).global = window;
(window as any).process = {};
(window as any).process.nextTick = setTimeout;
Tympanic answered 28/8, 2018 at 13:57 Comment(1)
The pollyfills sorted it out for meEpiphany
W
2
import PouchDB from 'pouchdb';

export abstract class PouchDBDatabase {
    private _database: PouchDB.Database<Sheet>;

    constructor(protected DATABASE_URL: string) {
        this._database = new PouchDB(this.DATABASE_URL);
    }
}

auto-completion with typescript

And you're good to go with typescript + pouchDB :)

Wei answered 3/12, 2020 at 8:54 Comment(0)
T
0

I managed to get the module recognised by using

declare function require(a)

var PouchDB = require("pouchdb")

I have given up type checking, but at least I can make progress.

Transship answered 3/6, 2016 at 13:46 Comment(1)
Have you had any luck? i have the same issue, it's kinda working but need to reload page on new browser for data to appear. @LeRoy i've tried your method with no luck. Lot's of people having the same issue. I'm using angular2.Pizarro

© 2022 - 2024 — McMap. All rights reserved.