简体   繁体   中英

Using lowdb results in ERR_REQUIRE_ESM

I'm trying to use LowDB in a Node and Typescript project, but it keeps giving me an ES Module error...

Error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\{PATH_TO}\node_modules\lowdb\lib\index.js
require() of ES modules is not supported.
require() of C:\{PATH_TO}\node_modules\lowdb\lib\index.js from C:\{PATH_TO}\src\index.ts 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:\{PATH_TO}\node_modules\lowdb\package.json.

I'm using a tsconfig.json that was generated using tsc --init and it has these options:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

Here's my index.ts, directly copied from lowdb. Interestingly, it only throws this error when i import JSONFile .

import { join } from "path";
import { Low, JSONFile } from "lowdb";

const file = join(__dirname, "db.json");
const adapter = new JSONFile(file);
const db = new Low(adapter);

The command I use to run this is nodemon --watch "src/**" --ext "ts,json" --exec "ts-node src/index.ts"

I have been reading GitHub issues all morning, and I can't seem to figure this issue out.

I made a GitHub Repo , so you can download and try it out.

I'm on Node v16.x. Thanks in advance for any help!

EDIT: I said F-it, and wrote my own json database...

// lowdb was thorwing errors, so I wrote this one as a stand-in

import path from "path";
import fspromises from "fs/promises";
import fs from "fs";

// path to json file
const FILE_PATH = path.join(__dirname, "database.json");

export const initJSONDatabase = <T>(initialData: T) => {
  const read = async () => {
    const data = await fspromises.readFile(FILE_PATH, { encoding: "utf-8" });
    return JSON.parse(data) as unknown as T;
  };

  const write = async (data: T) => {
    await fspromises.writeFile(FILE_PATH, JSON.stringify(data), {
      encoding: "utf-8",
    });
  };

  if (!fs.existsSync(FILE_PATH)) {
    write(initialData);
  }

  return {
    read,
    write,
  };
};

// -- Usage --
// 
// const defaultState = {
//   users: [], 
//   posts: []
// }; 
// const db = initJSONDatabase(defaultState); 
// const data = await db.read(); 
// data.users.push('Jay-Z'); 
// await db.write(data); 

I had the problem with using require() with node v 12.20.0 . I use node v 12.18.3 and,

 "dependencies": {
    "lowdb": "^1.0.0", //the newest version had the problem
    "socket.io": "^3.1.0",
    "supervisor": "^0.12.0"
  },

and now it is working!

I was having the same issue with "target": "ES2020" , someone already did a fix that is working for me just fine: https://github.com/typicode/lowdb/issues/471#issuecomment-850908461

Using lowdb@3.0.0 and these options in tsconfig.json fixes the problem.

{
  "compilerOptions": {
    "moduleResolution": "node",
    "target": "es6",
    "module": "es6"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM