2
votes

I am trying to install pouchdb in a typescript (ionic) application. The types don't work

npm install --save pouchdb
npm install --save-dev @types/pouchdb

When I try to use it (import Pouchdb from 'pouchdb'), I get this error

ERROR in src/app/services/pouchdb.service.ts(3,8): error TS1192: Module '"C:/Users/User/PROG/toto/node_modules/@types/pouchdb/index"' has no default export.

I tried

import * as Pouchdb from 'pouchdb'

The error disappears here, but appears after, I still can't use the pouchdb functions.

Is there a solution?

3
PouchDB has an export assignment (export = ...), so you need to enable the esModuleInterop compiler option if you want import PouchDB from 'pouchdb' to work. If esModuleInterop is disabled, then the second approach should have worked (although import Pouchdb = require('pouchdb') would be better practice); if you'd like help with it, please post the exact error you got and the code it occurred in.Matt McCutchen

3 Answers

2
votes

Have you seen this? https://pouchdb.com/guides/setup-pouchdb.html#typescript

In tsconfig.json:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true
  }
}

Then in TypeScript:

import PouchDB from 'pouchdb';
0
votes
const PouchDB = require('pouchdb').default;

This worked for me.

0
votes

I also am using Ionic 4, PouchDB and pouchdb-find. The following worked for me:

npm install pouchdb @types/pouchdb

npm install pouchdb-find @types/pouchdb-find

In tsconfig.json I added the following:

{
  {
    "compilerOptions": {
        "allowSyntheticDefaultImports": true
  }
}

Then in the provider where I'm using pouchdb and pouchdb-find, I put the following 2 lines:

const PouchDB = require('pouchdb');
PouchDB.plugin(require('pouchdb-find'));