lowdb: Can't import JSONFile from "lowdb/node" import.
When I use import {JSONFile} from 'lowdb/lib/node'
it return the following error message.
[ERROR] Could not resolve "lowdb/lib/node"
src/users.ts:2:23:
2 │ import {JSONFile} from 'lowdb/lib/node'
╵ ~~~~~~~~~~~~~~~~
The path "./lib/node" is not exported by package "lowdb":
I’m using Esbuild, I tried to make this package external, or tried find another import path, but I can’t make it work without modifying the lowdb source code. This is what I did :
index.js
export * from './adapters/Memory.js';
export * from './adapters/MemorySync.js';
export * from './Low.js';
export * from './LowSync.js';
+ export * from './node.js';
I tried to use the documentation path import {JSONFile} from 'lowdb/node'
but typescript can’t find the module.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 37
- Comments: 42 (8 by maintainers)
Commits related to this issue
- Upgrade lowdb - https://github.com/typicode/lowdb/issues/554#issuecomment-1345252506 — committed to kamilmielnik/cv by kamilmielnik a year ago
For we works like this
Same issue here with plain Typescript. This is a deal breaker for me. Something so basic (even shown in the official tutorial) should work with absolutely no issues. Abstract this problem away. Too much configuration from the get-go
have the same issue, unfoutrnly setting moduleResolution to ‘nodenext’ is casuing other issues in my application 😦
I believe this has something to do with the ts es module resolution strategy. I ran into the issue too, but I switched my project to pure esm config later and everything works. I suggest using the latest typescript version and setting
moduleResolution
toNodeNext
in yourtsconfig.json
:I ran a quick test using @tkafka 's repo: https://github.com/tkafka/lowdb-node-fail
The default
moduleResolution
isnode
, if you runnpx tsc --noEmit --traceResolution | grep -A50 -m1 lowdb/node
, the output will be something like this:If you change
moduleResolution
tonodenext
, and run the trace command again, the output will be:which suggests the module resolution succeeded.
And if someone doesn’t add
type: "module"
in hispackage.json
or doesn’t use.mjs
file extensions, which means he wants to import an es module in a commonjs module, then he will have to use dynamic import: https://stackoverflow.com/a/70396888/4668057@Smiley43210 You should use
nodenext
formoduleResolution
intsconfig.json
, like I said here: https://github.com/typicode/lowdb/issues/554#issuecomment-1293154430Same problem in NestJS (not NextJS). Changing moduleResolution breaks all other imports.
Can you try setting
"type": "module"
in your package.json? Here’s also how you can test locally:package.json
tsconfig.json
src/index.ts
yeah I second @weoreference First I thought this package would be very nice to have as mock database on local machine. But just trying to install / fix
import { JSONFile } from 'lowdb/node';
is just breaking everything in my code. to bad 😦
It’s totally unusable. If we don’t fix this
Another option is to patch up lowdb (and leave everything else alone):
npm i -D patch-package
(if you don’t already have it)export * from './node.js'
to the end ofnode_modules/lowdb/lib/index.js
andnode_modules/lowdb/lib/index.d.ts
npx patch-package lowdb
"postinstall": "patch-package"
to scripts in package.json (if you don’t already have this or something to that effect)The last step ensures that everytime you reinstall modules you will get that patch reapplied.
You will now be importing JSONFile from lowdb, ie.
import { Low, JSONFile } from 'lowdb';
not work in electron
Still have some problem in electron
it works now @typicode 😃
Could you please retry with lowdb v6.0.1 which contains @Krak798’s fix #569
I think this is related to https://github.com/microsoft/TypeScript/issues/50794
Just as @GrinZero did I got it working but with
I took this plan back, not because I couldn’t run. It’s because I really don’t think it’s necessary to insist on using such a bad type system, and the demo can’t be opened
I’m able to reproduce this with just lowdb + TypeScript. See https://github.com/Smiley43210/cannot-find-lowdb for minimum repro.
lowdb@5.05 typescript@4.8.4
Both
nodenext
andNodeNext
are fine 😃That’s disappointing. My guess is that there are some inconsistent behaviours.
There seems to be already several issue and discussions: https://github.com/vercel/next.js/issues/35572 https://github.com/vercel/next.js/discussions/41189