Node.js 使用lowdb会导致ERR\u REQUIRE\u ESM

Node.js 使用lowdb会导致ERR\u REQUIRE\u ESM,node.js,typescript,lowdb,Node.js,Typescript,Lowdb,我试图在一个节点和类型脚本项目中使用,但它一直给我一个ES模块错误 错误: 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:

我试图在一个节点和类型脚本项目中使用,但它一直给我一个ES模块错误

错误:

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.
我正在使用一个
tsconfig.json
,它是使用
tsc--init
生成的,它有以下选项:

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

这是我的index.ts,直接从lowdb复制。有趣的是,它只在我导入
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);
我用来运行它的命令是
nodemon--watch“src/**”-ext“ts,json”--exec“ts node src/index.ts”

我整个上午都在阅读GitHub的问题,但我似乎无法理解这个问题

我做了一个,所以你可以下载并试用

我在节点v16.x上。提前感谢您的帮助

编辑: 我说了F-it,并编写了自己的json数据库

// 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); 
//lowdb出现了错误,所以我写了这篇文章作为替身
从“路径”导入路径;
从“fs/promises”导入fspromises;
从“fs”导入fs;
//json文件的路径
const FILE_PATH=PATH.join(u dirname,“database.json”);
export const initJSONDatabase=(initialData:T)=>{
const read=async()=>{
const data=await fspromises.readFile(文件路径,{编码:“utf-8”});
返回JSON.parse(数据),与T一样未知;
};
常量写入=异步(数据:T)=>{
等待fspromises.writeFile(文件路径,JSON.stringify(数据){
编码:“utf-8”,
});
};
如果(!fs.existsSync(文件路径)){
写入(初始化数据);
}
返回{
阅读
写
};
};
//--用法--
// 
//const defaultState={
//用户:[],
//员额:[]
// }; 
//const db=initJSONDatabase(defaultState);
//const data=await db.read();
//data.users.push('Jay-Z');
//等待数据库写入(数据);