typicode / lowdb

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

Import issue on typescript #557

Closed aminlatifi closed 1 year ago

aminlatifi commented 1 year ago

By importing lowdb in my typescript project I get below error

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/amin/Work/giveth/giveconomy-notification-service/node_modules/lowdb/lib/index.js from /Users/amin/Work/giveth/giveconomy-notification-service/src/repository/database.ts not supported.
Instead change the require of index.js in /Users/amin/Work/giveth/giveconomy-notification-service/src/repository/database.ts to a dynamic import() which is available in all CommonJS modules.

Related dependencies:

ts-node: "^10.9.1", typescript: "^4.9.3", Nodejs 16

tsconfig: ... "module": "commonjs" / Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. /, "lib": [ / Specify library files to be included in the compilation. / "es2018", "es2019", "esnext.asynciterable" ], ....

setting type:module in package.json didn't solve the problem

typicode commented 1 year ago

Add "module": "node16", "moduleResolution": "node16" to your tsconfig.json. (Example)

Darkmift commented 1 year ago

reporting same issue as @aminlatifi attempt to modify tsconfig as recommended did not resolve the issue.

megaacheyounes commented 1 year ago

facing same issue..

typicode commented 1 year ago

There's this resource that can help as well https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c