无法在我的项目中导入PouchDB
问题描述:
我正在开发一个项目,最近PouchDB决定停止工作。我不能在我的ts项目中编译它。我想:无法在我的项目中导入PouchDB
import * as PouchDB from 'pouchdb';
export default PouchDB.defaults({
prefix: `http://admin:[email protected]: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:[email protected]: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:[email protected]:5984/`
} as any)
然后,它会编译,但我得到这个当我运行它:
export default PouchDB.defaults({
^
TypeError: Cannot read property 'defaults' of undefined
at Object.<anonymous> ...
如果我尝试:
const PouchDB = require('pouchdb');
我失去了所有的类型,所以就变成了any
型我OBV不想要
❯ npm ls pouchdb @types/pouchdb
[email protected] /mnt/c/Users/Vincent/Documents/GitHub/compactd
├── @types/[email protected]
└── [email protected]
我停止工作之前做的唯一事情是清理node_modules并重新启动所有使用npm而不是纱线(纱线希望重新编译所有我的本地模块,每次我安装的东西,但我觉得npm是这样做的)
我试着降级PouchDB到6.2.0,我疯狂的任何帮助将非常感激。
使用TS-节点,我试图要求/进口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_modules文件夹中有一个PouchDB的旧版本,并且它的界面已更改。也许尝试在'node'或'ts-node' REPL中导入(或要求)该模块?然后,您可以记录您的PouchDB对象,并查看其上可用的属性。如果你的类型定义不匹配,这不会解决问题,但也许它可以帮助找到排队的PouchDB和@ types/PouchDB版本 –
@NathanWilson调试了很多之后,我发现我想要的定义在自6.0.5以来的'pouchdb/lib/index.es'中。当然,我不能直接导入.es文件,没有发现类型。试图安装6.0.4现在... – Vinz243
Nvm,仍然无法使用6.0.4 ... – Vinz243