typicode / lowdb

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

JSONFile path error #575

Closed carldegs closed 10 months ago

carldegs commented 11 months ago

I am using lowdb as a temporary db for my web app made in Vite. I'm trying to use the JSONFile adapter but I am getting the following error:

Uncaught TypeError: (0 , import_node_path.dirname) is not a function
    at getTempFilename (index.js:18:17)
    at new Writer (index.js:33:60)
    at new TextFile (TextFile.js:22:56)
    at new JSONFile (JSONFile.js:17:57)
    at db.ts:22:17

And this is how I've setup my db:

import { Content } from '@/types/Content';
import { ExpChain } from 'lodash';
import chain from 'lodash/chain';
import { Low } from 'lowdb';
import { JSONFile } from 'lowdb/node';

interface Data {
  contentList: Record<string, Content>;
}

const defaultData: Data = {
  contentList: {},
};

class LowWithLodash<T> extends Low<T> {
  chain: ExpChain<this['data']> = chain(this).get('data');
}

const adapter = new JSONFile<Data>('public/mock/db.json');
const db = new LowWithLodash<Data>(adapter, defaultData);

export default db;

Libraries used: vite@4.3.9 lowdb@6.0.1

theavitw commented 11 months ago

Can I work on this issue ?

typicode commented 11 months ago

If it's running in the browser, then you should use the local storage adapter (see docs) alternatively you can create an API that runs on Node, this way you can use the JSONFile adapter.