typicode / lowdb

Simple and fast JSON database
MIT License
21.35k stars 918 forks source link

Using lowDB in the browser #515

Closed rvt closed 2 years ago

rvt commented 2 years ago

I have been trying to add lowdb in a rollup web project based on open-wc with typescript and I am getting this as soon as my page loads (compilation is fine...)

Web Dev Server started...

  Root dir: /opt/source/test
  Local:    http://localhost:8000/
  Network:  http://192.168.178.106:8000/

Error while transforming node_modules/lowdb/lib/adapters/TextFileSync.js: Could not resolve import "fs".

  11 | };
  12 | var _TextFileSync_tempFilename, _TextFileSync_filename;
> 13 | import fs from 'fs';
     |       ^
  14 | import path from 'path';
  15 | export class TextFileSync {
  16 |     constructor(filename) {

Error while transforming node_modules/lowdb/lib/adapters/TextFile.js: Could not resolve import "fs".

  11 | };
  12 | var _TextFile_filename, _TextFile_writer;
> 13 | import fs from 'fs';
     |       ^
  14 | import { Writer } from 'steno';
  15 | export class TextFile {
  16 |     constructor(filename) {

In my project I have something like this:

import { LocalStorage } from 'lowdb';
..
...

export class ConnectionSettings extends LitElement {
....

connectedCallback() {
    super.connectedCallback();
    const adapter = new LocalStorage('db')
  }

....
}
shennan commented 2 years ago

I'm guessing that TextFile and TextFileSync should not be used in the browser as they are for reading the file system. Check you're not importing them anywhere in your code. If you're not, then perhaps you have an over-enthusiastic compiler that is attempting to transform everything in the library?

rvt commented 2 years ago

Okie dokie, I think I can add an exclude for these files... thanks!