Monday, 1 April 2019

Persist nedb to disk in Electron renderer process (Webpack/Electron/nedb configuration problem)

Problem

I'm trying to use a pure-JS database called nedb in an Electron renderer process. It uses the browser field in its package.json to swap in a browser-based storage system. This is causing my database to not actually be persisted to file.

Background

I'm using Next.js as my view framework, and its Webpack is configured for "target": "electron-renderer" for the rendering thread. Which apparently causes Webpack to process those browser directives, even though renderer processes should have access to both browser and Node APIs. This behavior isn't really documented, so I don't know how to override it.

What I have tried

I have confirmed that if I manually edit out the browser field on the local copy of node_modules/nedb/package.json, the problem goes away.

As a temporary workaround, I've pointed to my own fork of nedb that does this. But this is pretty unsatisfactory.

Other research

Curiously, this doesn't seem to be an issue for electron-vue, whose docs explicitly demonstrate use of nedb from a renderer process. That framework does, indeed, appear to use "target": "electron-renderer" in its Webpack config.

Is there a solution to this problem, perhaps by Webpack configuration?



from Persist nedb to disk in Electron renderer process (Webpack/Electron/nedb configuration problem)

No comments:

Post a Comment