typicode / lowdb

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

ERR_REQUIRE_ESM : Must use import to load ES Module typescript error #488

Closed luluhoc closed 3 years ago

luluhoc commented 3 years ago
node:internal/modules/cjs/loader:1112
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\admin\Desktop\goldprice\node_modules\lowdb\lib\index.js
require() of ES modules is not supported.
require() of C:\Users\admin\Desktop\goldprice\node_modules\lowdb\lib\index.js from C:\Users\admin\Desktop\goldprice\build\routes\api\price.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\admin\Desktop\goldprice\node_modules\lowdb\package.json.

    at new NodeError (node:internal/errors:363:5)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:13)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.<anonymous> (C:\Users\admin\Desktop\goldprice\build\routes\api\price.js:8:17)     
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Module.load (node:internal/modules/cjs/loader:975:32) {
  code: 'ERR_REQUIRE_ESM'
}

node 16 "lowdb": "^2.1.0"

typicode commented 3 years ago

See https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c :)

SwapnilSoni1999 commented 2 years ago

Still doesnt provides solution