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
相关产品推荐
相关产品推荐

