无法在我的项目中导入PouchDB

时间:2017-09-26 00:41:45

标签: node.js typescript pouchdb typescript-typings

我正在开展一个项目,最近PouchDB决定停止工作。我无法在我的ts项目中编译它。我试过了:

import * as PouchDB from 'pouchdb';

export default PouchDB.defaults({
  prefix: `http://admin:password@localhost:5984/`
})

但我得到

server/src/database.ts(3,24): error TS2339: Property 'defaults' does not exist on type 'typeof 'pouchdb''.

如果我尝试:

import {PouchDB} from 'pouchdb';

export default PouchDB.defaults({
  prefix: `http://admin:password@localhost:5984/`
} as any)

我得到了

server/src/database.ts(1,9): error TS2305: Module ''pouchdb'' has no exported member 'PouchDB'.

如果我尝试:

import PouchDB from 'pouchdb';

export default PouchDB.defaults({
  prefix: `http://admin:password@localhost:5984/`
} as any)

然后它会编译,但是当我运行它时我会得到它:

export default PouchDB.defaults({
                      ^
TypeError: Cannot read property 'defaults' of undefined
    at Object.<anonymous> ...

如果我尝试:

 const PouchDB = require('pouchdb');

我输掉了所有的输入,所以它变成any类型,我不想要

❯ npm ls pouchdb @types/pouchdb
compactd@0.0.0 /mnt/c/Users/Vincent/Documents/GitHub/compactd
├── @types/pouchdb@6.3.0
└── pouchdb@6.3.4

我在停止工作之前唯一做的就是清理node_modules并使用npm而不是yarn重新安装(每次安装时我想重新编译所有本机模块,但我觉得npm也是这样做的)

我尝试将PouchDB降级到6.2.0,我已经尝试了所有这些让我疯狂的任何帮助都会非常感激。

使用ts-node,我试图尽可能地要求/导入PouchDB:

> require('pouchdb')
{ [Function: PouchDB$5]
  super_:
   { [Function: AbstractPouchDB]
     super_:
      { [Function: EventEmitter]
        EventEmitter: [Object],
        usingDomains: true,
        defaultMaxListeners: [Getter/Setter],
        init: [Function],
        listenerCount: [Function] } },
  adapters:...

> import PouchDB from 'pouchdb'
undefined
> PouchDB
undefined

> import * as PouchDB from 'pouchdb'
[eval].ts:2
Object.defineProperty(exports, "__esModule", { value: true });
                      ^

ReferenceError: exports is not defined
    at [eval].ts:2:23
    at ContextifyScript.Script.runInContext (vm.js:53:29)
    at ContextifyScript.Script.runInNewContext (vm.js:59:15)
    at _eval (/home/vincent/.nvm/versions/node/v8.5.0/lib/node_modules/ts-node/dist/_bin.js:181:29)
    at REPLServer.replEval (/home/vincent/.nvm/versions/node/v8.5.0/lib/node_modules/ts-node/dist/_bin.js:220:18)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:440:10)
    at emitOne (events.js:115:13)
    at REPLServer.emit (events.js:210:7)

> import {PouchDB} from 'pouchdb'
Thrown: ⨯ Unable to compile TypeScript
[eval].ts (1,9): Module ''pouchdb'' has no exported member 'PouchDB'. (2305)

1 个答案:

答案 0 :(得分:0)

截至目前,最好的解决方案是:

import Pouch from 'pouchdb';
const PouchDB: typeof Pouch = require('pouchdb');

这很快,很脏,但真正的修复需要由pouchDB维护者

完成
相关问题