lowdb: node@14.16.0 Error [ERR_REQUIRE_ESM]

node: 14.16.0 electron: 13.0.1 webpack: 4.46.0

import { Low, JSONFile } from 'lowdb'

const adapter = new JSONFile('example.json')

export const lowdb = new Low(adapter)

The above code run error, ERR_REQUIRE_ESM has been prompted, what is the solution?

image

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 12
  • Comments: 19 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I opened a PR opened here to address this issue.


You can test it by installing the package from PR’s lib branch

npm

npm install github:dankreiger/lowdb#chore/esm-cjs-hybrid-WITH-LIB 

yarn

yarn add github:dankreiger/lowdb#chore/esm-cjs-hybrid-WITH-LIB

Closing as I prefer to keep v2 a pure ESM package. I’m aware not every project can migrate to ESM right now, but tools and libraries are catching up. If possible, I’d recommend migrating your project to ESM.

For anyone looking to fix the error, please follow this excellent guide: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

Here some solutions:

  1. Download source code zip, change into "module": "commonjs" in tsconfig.json, run build and put project into your node_modules, you can remove src dict manually.
  2. Maintaining by youself and put into your packages if you are using lerna to manage your project.
  3. Hey, dear author. Could you publish a package with commonjs for my piteous ts-node, whose esm support is Experimental. Please, and thank you!

I’m having the same issue here…

Convenient timing, I’m also seeing this issue.

I have the same problem.