-
-
Notifications
You must be signed in to change notification settings - Fork 917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't import JSONFile from "lowdb/node" import. #554
Comments
Hi, Could you please check that you're using latest lowdb version? I have it working with v5.0.5 and TypeScript in a project I'm working on. |
@typicode |
I use the latest 5.0.5 version. |
Issue happens on the lowdb/browser imports too. I can reproduce using a default Next.js project + lowdb: https://github.com/jasonbarone/lowdb-type-error-test. |
Can you try setting
{
"name": "lowdb-test",
"version": "1.0.0",
"type": "module",
"dependencies": {
"lowdb": "^5.0.5"
},
"devDependencies": {
"@sindresorhus/tsconfig": "^3.0.1",
"typescript": "^4.8.4"
}
}
{
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"outDir": "./lib"
}
}
import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node' /tmp/lowdb-test is 📦 v1.0.0 via v19.0.0
❯ npm x tsc
src/index.ts:1:1 - error TS6133: 'Low' is declared but its value is never read.
1 import { Low } from 'lowdb'
~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/index.ts:2:1 - error TS6133: 'JSONFile' is declared but its value is never read.
2 import { JSONFile } from 'lowdb/node'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 2 errors in the same file, starting at: src/index.ts:1 |
Still not working. For
If I do |
Hmm I don't know why you're still getting this error. You can check https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#im-having-problems-with-esm-and-typescript Mapping from |
+1 here - typescript project with Next.js.
It seems that the assignment |
It's totally unusable. If we don't fix this |
@tkafka it should be That said, it seems to be tricky with Next.js for some reasons. |
@typicode Yes, it doesn't work with Next.js - I have made a simplest possible repo to reproduce this: https://github.com/tkafka/lowdb-node-fail Meanwhile, I keep using |
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 {
"compilerOptions": {
"moduleResolution": "NodeNext",
}
} I ran a quick test using @tkafka 's repo: https://github.com/tkafka/lowdb-node-fail The default
If you change
which suggests the module resolution succeeded. And if someone doesn't add |
@Sec-ant Thank you for the research! I am not sure if the case matters, but the documentation says it should be However, the build still seems to fail:
Do you think I should report this to Next.js instead? |
Both
That's disappointing. My guess is that there are some inconsistent behaviours.
There seems to be already several issue and discussions: vercel/next.js#35572 vercel/next.js#41189 |
I think it would be worth it reporting to Next.js if there's no duplicate issue already. In any case, AFAICT, lowdb works with Node + TypeScript with the example provided above and package.json config is correct. So I'm not sure what can be done at lowdb's level to make it work with Next.js. It's fine to stay on v3 meanwhile. v5 is a maintenance update and fixes issues with front-end tools like Vite. |
I'm able to reproduce this with just lowdb + TypeScript. See https://github.com/Smiley43210/cannot-find-lowdb for minimum repro. lowdb@5.05 |
@Smiley43210 You should use |
Sorry, I got confused and thought that was something specific to Next.js |
Same problem in NestJS (not NextJS). Changing moduleResolution breaks all other imports. |
Same problem here. Setting |
This breaks with V3 as well. |
try this solution: useageimport { JSONFile } from 'lowdb/node'
import type { JSONFile as JSONFileType } from 'lowdb/lib/node'
type DBData = {
hosts: string[]
}
const adapter = new JSONFile<DBData>(path.join(USER_DATA_PATH, 'db.json')) as JSONFileType<DBData> .d.tsdeclare module 'lowdb/node' {
export function JSONFile<T>(path: string): void
}
// you can add more |
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 |
Same issue with this import: |
I think this is related to microsoft/TypeScript#50794 Just as @GrinZero did I got it working but with declare module 'lowdb/node' {
export * from 'node_modules/lowdb/lib/node';
} |
For we works like this
|
I can confirm @joycollector's solution works for me as well, thank you! For the record, I have put |
Same problem |
Another option is to patch up lowdb (and leave everything else alone):
The last step ensures that everytime you reinstall modules you will get that patch reapplied. You will now be importing JSONFile from lowdb, ie. |
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 |
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
is just breaking everything in my code. to bad :( |
Could you please retry with lowdb v6.0.1 which contains @Krak798's fix #569 |
it works now @typicode :) |
Still have some problem in electron |
not work in electron |
could you please give more information about the error? |
I working in erb project,erb if I call lowdb in main process of electron ,it will throw error like this require() of es module demo 【https://github.com/colining/erb-lowdb-test】 please see db.ts and
@Krak798 thanks a lot |
|
Error in splitPdfPathHandler: [Error: ENOENT: no such file or directory, rename 'C:\Users\jakee\OneDrive\Desktop\lis-scan-docs\desktop-scan-docs\db.scan.json.tmp' -> 'C:\Users\jakee\OneDrive\Desktop\lis-scan-docs\desktop-scan-docs\db\scan.json'] { i am getting this error while processing files using electron application, how can i resolve this issue. |
When I use
import {JSONFile} from 'lowdb/lib/node'
it return the following error message.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
I tried to use the documentation path
import {JSONFile} from 'lowdb/node'
but typescript can't find the module.The text was updated successfully, but these errors were encountered: