2

I'm getting the error ('this.fs.stat is not a function') on line 2256 of lokijs.js. Environment is Electron with Angular 5.x and LokiJS v1.5.3.

fs is set up on line 2244 of lokijs.js (this.fs = require('fs');) but when I examine this.fs in the browser tools immediately after that line executes, it is just an empty object.

I've been beating my head against LokiJS all afternoon and have managed to get this far, but have no idea where to go from here. fs has been in node since forever, so I don't think it's an issue with a node version.

I'm aware of the issues with require and webpack, but since this is happening in LokiJS, I'm not sure whether this is related and if so what the proper fix would be.

Any ideas/suggestions?

Thanks.

Update #1

Code:

import * as Loki from 'lokijs';
import { LokiFsAdapter } from 'lokijs';
this.adapter = new LokiFsAdapter();
this.db = new Loki('my.db', {
      env: 'NODEJS',
      autoload: true,
      autosave: true,
      verbose: true,
      autoloadCallback: this.initCollections
    });

This gets me the error I mentioned above. If I switch the first line to import { Loki } from 'lokijs'; I get this error instead: Uncaught (in promise): TypeError: lokijs_1.Loki is not a constructor

TimTheEnchanter
  • 3,370
  • 1
  • 26
  • 47
  • Code? Looks like you are trying to use a fs adapter in a browser context, in which case LokiJS would correctly say that the global object does not have a fs property – Joe Minichino Mar 17 '18 at 07:34
  • Thanks, Joe. Added code. This code is in the renderer process, but in Electron, node modules are available there, so fs should be. I'm using fs successfully in another renderer process. – TimTheEnchanter Mar 17 '18 at 13:54
  • Are you using any electron adapter for angular? Like `ngx-electron` ? – j4v1 Mar 22 '18 at 14:00
  • No adapter. Just straight Angular. – TimTheEnchanter Mar 22 '18 at 18:18
  • I got the exact same problem: My impression is that Loki does not recognize electron's inclusion of the default node package `fs`, but I can require such package through electron.remote. I would think that the `new Loki` definition should allow an optional parameter that can be used to define `fs` visible for Loki ? _anyway, I am moving to use loki in the main ipc process, instead of renderer (angular), and define some interprocess comms/events_ – j4v1 Mar 23 '18 at 22:54

0 Answers0