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.

user2302244
  • 843
  • 2
  • 11
  • 27

5 Answers5

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.

leetheguy
  • 872
  • 10
  • 29
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

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;
Jeremy
  • 3,620
  • 9
  • 43
  • 75
1
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 :)

Danny
  • 305
  • 3
  • 12
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.

user2302244
  • 843
  • 2
  • 11
  • 27
  • 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. – Rodrigo Rubio Oct 30 '16 at 12:33