node.js - 无法再在我的项目中导入 PouchDB

标签 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');

我丢失了所有输入内容,因此它变成了我不想要的任何类型

❯ 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希望每次我安装某些东西时重新编译我的所有 native 模块,但我觉得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)

最佳答案

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

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

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

关于node.js - 无法再在我的项目中导入 PouchDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46416146/

相关文章:

javascript - 对象函数 router(req, res, next) { router.handle(req, res, next); } 没有方法

javascript - 将乱码转换为ISO-8859-1

typescript - 是否有 ESLint 规则来防止真实检查

javascript - 存储一个可以在应用程序中随时更新的对象 angular 5

css - 为什么 node-sass 生成的 CSS 文件不在第一个 GET 请求中加载?

angular - 如何在 Angular 7 中使用路由器导航进行滚动?

pouchdb - 显示 pouchdb 中的总行数

couchdb - 按用户角色划分的 Couchdb/Couchdb 复制策略

CouchDB/PouchDB 芒果查询 : has in array

node.js - Volta 与yarn run 构建系统找不到指定的路径