typicode / lowdb

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

Cannot find module 'lowdb/node' or its correspoing type declaractions #568

Closed nulkode closed 1 year ago

nulkode commented 1 year ago

Code

import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node'
import { join } from 'path';
import { rootDir } from './constants';

const adapter = new JSONFile(
    join(rootDir, 'database', 'db.json')
);
const defaultData = { users: [] };
const db = new Low(adapter, defaultData);

export default db;

Packages

typescript: 5.0.4

Typescript config

{
      "compilerOptions": {
        "target": "ES2022",
        "module": "NodeNext",
        "moduleResolution": "node",
        "baseUrl": "./src",
        "sourceMap": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "skipLibCheck": true,
        "outDir": "./dist"
      },
      "include": ["src/**/*"]
}

Error message

$ tsc
src/lib/database.ts:2:26 - error TS2307: Cannot find module 'lowdb/node' or its corresponding type declarations.

Expected behaviour

The message should not be shown and the app should run.

nulkode commented 1 year ago

I don't know if this is a duplicate of #554 but this has never happend to me before.

typicode commented 1 year ago

I think it's probably related to the config. I have a working app with lowdb v6, TS v5, node v19.9.0 and the following config:

{
  "extends": "@sindresorhus/tsconfig",
  "compilerOptions": {
    "incremental": true,
    "outDir": "./lib",
    "skipLibCheck": true
  }
}

If you can't change your config, you can have a look at the workaround in #554

BTW, it's because lowdb uses this https://nodejs.org/api/packages.html#subpath-exports

typicode commented 1 year ago

Many thanks for the PR :+1:

nulkode commented 1 year ago

I'm very happy to help.

This was my first time contributing to a serious open source project.