typicode / lowdb

Simple and fast JSON database
MIT License
21.58k stars 924 forks source link

Uncaught TypeError: (0 , import_node_path.dirname) is not a function #586

Closed borzaka closed 11 months ago

borzaka commented 11 months ago

My stack: Vite + React + TS

The small TypeScript example is not working:

import { JSONFilePreset } from 'lowdb/node'

type Data = {
    messages: string[]
}

const defaultData: Data = { messages: [] }
const db = await JSONFilePreset<Data>('db.json', defaultData)

db.data.messages.push('foo')

The error:

lowdb_node.js?v=0050ebbd:74 Uncaught TypeError: (0 , import_node_path.dirname) is not a function
    at getTempFilename (lowdb_node.js?v=0050ebbd:74:66)
    at new Writer (lowdb_node.js?v=0050ebbd:104:39)
    at new TextFile (lowdb_node.js?v=0050ebbd:162:33)
    at new DataFile (lowdb_node.js?v=0050ebbd:218:34)
    at new JSONFile (lowdb_node.js?v=0050ebbd:266:5)
    at JSONFilePreset (lowdb_node.js?v=0050ebbd:335:42)
    at App.tsx:12:18

Maybe it's only available on the server side? Also, this warning comes as well:

Module "node:path" has been externalized for browser compatibility. Cannot access "node:path.join" in client code. See https://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.

typicode commented 11 months ago

Maybe it's only available on the server side?

Yes, it's only available server side (e.g. Express server). For client side, import from lowdb/browser.