【问题标题】:Cannot import anymore PouchDB in my project无法在我的项目中再导入 PouchDB
【发布时间】:2018-03-07 01:39:12
【问题描述】:

我正在做一个项目,最近 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 重新安装(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)

【问题讨论】:

  • 我的猜测是您的 node_modules 文件夹中有旧版本的 PouchDB,并且它的界面已更改。也许尝试在nodets-node REPL 中导入(或要求)该模块?然后,您可以记录您的 PouchDB 对象并查看其上可用的属性。如果您的类型定义不匹配,这不会解决问题,但也许它可以帮助找到一个 PouchDB 和 @types/PouchDB 的版本
  • @NathanWilson 经过大量调试,我发现我想要的定义在 pouchdb/lib/index.es 中,它从 6.0.5 开始就存在。当然,我不能直接导入 .es 文件,没有找到任何类型。正在尝试安装 6.0.4 ...
  • Nvm,仍然不能与 6.0.4 一起使用 ...

标签: node.js typescript pouchdb typescript-typings


【解决方案1】:

到目前为止,最好的解决方法是:

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

这又快又脏,但真正的修复需要 pouchDB 维护者来完成

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多