pouchdb: PouchDB not working with Angular 6.0.0
Issue
PouchDB and PouchDBFind do not work with Angular 6.0.0, throwing an error as soon as a new PouchDB instance is created, or as soon as an index is created.
Info
- Environment: Angular 6.0.0 (Angular CLI 6.0.0 and Node.js v8.9.4)
- Platform: Chrome 66.0.3359.139 (64 bits)
- Adapter: IndexedDB
- Server: No server needed to reproduce the error
Reproduce
This simple example should be enough to reproduce the issue: https://stackblitz.com/edit/angular-rgm8ga. It does not crash on stackblitz but it does if the project is built with the Angular CLI 6.0.0 (simply run ng serve
)
I believe the problem is related with the fact that the Angular CLI 6.0.0 stopped providing shims for global
and process
. See this comment: https://github.com/angular/angular-cli/issues/9827#issuecomment-369578814
The project compiles fine with the CLI but after opening the browser the error thrown is
when the PouchDB instance is created:
After some research, I added (window as any).global = window;
to polyfills.ts
, and the error thrown is now
when trying to create an index.
Any idea how to solve this issue? I have a project that relies heavily on PouchDB, but there are some other Angular-related libraries I’m using whose newest features are only avaliable for Angular 6, so I’d really like to update to Angular 6 while keeping PouchDB.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 12
- Comments: 25 (3 by maintainers)
The following in polyfills.ts will fix PouchDB with PouchDBFind plugin:
with
As suggested,
does solve the issue in Angular 6. It’s not ideal but it works 😉
The memory adapter also seems to have an issue with indirectly depending on stream via readable-stream:
@daleharvey The Angular CLI uses webpack to build javascript code. I don’t actually know how webpack works, but from what I understood, what happened is that the Angular CLI 6 removed the code that tells webpack to turn node.js built-ins like
global
andprocess
into code that the browser understands. (link to the commit) Their reasoning is that libraries should not assume the environment they’re running on, and if they’re intended to run both on node and the browser, then they should provide a separate web version. (I tried using pouchdb-browser btw, but I still got the same errors)I managed to fix the first error I posted above by adding
to
src/polyfills.ts
.The second error comes from PouchDB Find code, which calls
process.nextTick()
at some point when creating an index. (I saw that you closed an issue related to this, but the current release still usesprocess.nextTick()
instead ofsetTimeout
)Anyway, for the time being I managed to fix the second error by manually importing the code that webpack uses to polyfill
process
in the browser. I downloaded this file and saved it assrc/node-polyfills/process/polyfill.js
(I removed lines 16-24) and then insidesrc/polyfills.ts
added the following code:I understand this is a workaround, but until the Angular CLI provides a way to override the node webpack config it’ll have to do.
this works for me var PouchDB = require(‘pouchdb-browser’);
let pouchdb = PouchDB.default.defaults(); let db = new pouchdb(‘mydb’); db.info().then(info => console.log(info));
dist/pouchdb.find does seem to expect a global PouchDB object to be available.
You may have better luck with going back to
and then adding
to your polyfills.ts file as suggested here
@daleharvey this is worth reopening as it’s still a problem. a one line shim will fix this
these code work for me when i code in polymer3. thanks.