You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript使用better-sqlite3报错TypeError: better_sqlite3_1.default is not a constructor的问题求助

TypeScript使用better-sqlite3报错TypeError: better_sqlite3_1.default is not a constructor的问题求助

我在JavaScript里运行这段代码完全正常:

import Database from 'better-sqlite3';

const db = new Database('my.db');
const stmt = db.prepare('SELECT * FROM market');
for(const row of stmt.iterate()) {
    console.log(row["price"]);
}

console.log("Success");

但换成TypeScript运行时,就会抛出这个错误:

TypeError: better_sqlite3_1.default is not a constructor

我试过把导入语句改成 import * as Database from 'better-sqlite3';,虽然编译后执行JS文件不会再抛出TypeError,但又出现了新问题——better-sqlite3相关的代码完全没执行,程序直接就输出了"Success"。

我已经在配置里加了不少选项,这是我当前的package.json内容:

{
  "name": "nodejs-sqlite",
  "esModuleInterop": true,
  "moduleResolution": "node",
  "module": "commonjs",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "better-sqlite3": "^11.8.1",
    "sqlite3": "^5.1.7"
  }
}

有没有大佬知道这个报错的原因,以及怎么解决呀?

备注:内容来源于stack exchange,提问作者UsAA12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 14:23:02