typicode / lowdb

Simple and fast JSON database
MIT License
21.49k stars 923 forks source link

TypeScript: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module #482

Closed realjesset closed 3 years ago

realjesset commented 3 years ago

I am trying this library in TypeScript and I can't find a way to make it work.

// file database.ts
import { Low, JSONFile } from 'lowdb'
import { join } from 'path'

/** database path */
const db_path = join(__dirname, '../utils/database/db.json');

export type DatabaseType = {
    guilds: {}
    cache: {} 
}

/** database class to initialize the client database */
class Database {
    /** main database, use this to access the db */
    db: Low<DatabaseType>
    /** client based cache */
    public cache: Cache

    constructor(public bot: Client) {
        const adapter = new JSONFile<DatabaseType>(db_path)
        this.db = new Low<DatabaseType>(adapter);
        this.cache = this.db.data.cache;
        this.initialize()
    }

    /** initialize the database */
    private initialize() {
        this.db.data ||= { guilds: {}, cache: {} }
    }

    /** returns the full guild object
     * @param id guild id
     */
    public guilds(id: string) {
        return this.db.data.guilds[id];
    }

}

export default Database

I try to import this via

import Database from './database`

I get the [ERR_REQUIRE_ESM]: Must use import to load ES Module error message. I have tried to see all the possible answers but I am failing to understand the main concept.

// tsconfig.json
{
    "compilerOptions": {
      "module": "commonjs",
      "target": "ES2020",
      "noImplicitAny": false,
      "sourceMap": true,
      "outDir": "build",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "importHelpers": true,
      "forceConsistentCasingInFileNames": true,
      "resolveJsonModule": true,
      "esModuleInterop": true,
      "lib": [
        "ES2020",
        "esnext.asynciterable"
      ],
      "moduleResolution": "node"
    },
    "exclude": [
      "node_modules"
    ]
  }

I have changed the module/lib to ES2020 yet keep facing the same error. Any help is greatly appreciated. I will provide further specifications:

  node: 16.3.0
  npm: 7.15.1
  typescript: 4.2.4
  ts-node-dev: 1.1.6
  ts-node: 9.1.1

I have also added "type":"module" in package.json but I see it not working.

typicode commented 3 years ago

I'd try "module": "ES2020" in tsconfig.json.

There's a very good guide: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

oschade commented 3 years ago

have the same problem.

typicode commented 3 years ago

@oschade please see the guide above

ndmgrphc commented 3 years ago

Really wish the ESM only warning were more prominent. This is a massive undertaking for an existing project.

"Use our simple lowdb package! Lightweight! Yay!!!"

Also: "Replace all require()/module.export with import/export."

Just no...

safazi commented 3 years ago

Can't use with coffeescript anymore. Sad!

06000208 commented 3 years ago

This made my project no longer run, though this was on me for not doing my due dilligence when updating lowdb to it's next major version.

macizomedia commented 3 years ago

I got to fix the error and make the imports with js but still have another error

/home/mmcs/Templates/lowdb_server/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:397
  throw new ERR_PACKAGE_PATH_NOT_EXPORTED(
        ^
Error: ERR_PACKAGE_PATH_NOT_EXPORTED /home/mmcs/Templates/lowdb_server/node_modules/lowdb/ ./lib/adapters/JSONFile /home/mmcs/Templates/lowdb_server/node_modules/lowdb/lib/index.js
    at throwExportsNotFound (/home/mmcs/Templates/lowdb_server/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:397:9)
    at packageExportsResolve (/home/mmcs/Templates/lowdb_server/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:622:3)
    at packageResolve (/home/mmcs/Templates/lowdb_server/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:738:14)
    at moduleResolve (/home/mmcs/Templates/lowdb_server/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:815:18)
    at Object.defaultResolve (/home/mmcs/Templates/lowdb_server/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:929:11)
    at /home/mmcs/Templates/lowdb_server/node_modules/ts-node/src/esm.ts:68:38
    at Generator.next (<anonymous>)
    at /home/mmcs/Templates/lowdb_server/node_modules/ts-node/dist/esm.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/home/mmcs/Templates/lowdb_server/node_modules/ts-node/dist/esm.js:4:12)
[nodemon] app crashed - waiting for file changes before starting...

I'm using ts-node with a koa server I did implement the guide above.

Here is my ts.config

{
  "compilerOptions": {
    "target": "es2017",
    "module": "ESNext",
    "lib": [
      "es6"
    ] ,
    "allowJs": true,
    "outDir": "build",
    "rootDir": "src" ,
    "strict": true ,
    "noImplicitAny": true,
    "moduleResolution": "node",                 
    "esModuleInterop": true,

    /* Advanced Options */
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

Thanks for the Help!