denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
98.08k stars 5.4k forks source link

`better-sqlite3` doesn't work #26034

Open dector opened 1 month ago

dector commented 1 month ago

WORKAROUND

If you don't mind WAL mode - use libsql instead (it's compatible with better-sqlite3 API):

deno add npm:libsql-node



Original issue

This issue was raised earlier in #18444 and #19130 but was closed as "fixed". However, it's not.

$ deno --version
deno 2.0.0-rc.10 (release candidate, release, x86_64-unknown-linux-gnu)
v8 12.9.202.13-rusty
typescript 5.6.2

$ deno eval "import Database from 'npm:better-sqlite3'; new Database(':memory:')"
error: Uncaught (in promise) Error: Could not locate the bindings file. Tried:
 → /home/me/.cache/deno/npm/registry.npmjs.org/better-sqlite3/11.3.0/build/better_sqlite3.node
 → /home/me/.cache/deno/npm/registry.npmjs.org/better-sqlite3/11.3.0/build/Debug/better_sqlite3.node
 → /home/me/.cache/deno/npm/registry.npmjs.org/better-sqlite3/11.3.0/build/Release/better_sqlite3.node
 → /home/me/.cache/deno/npm/registry.npmjs.org/better-sqlite3/11.3.0/out/Debug/better_sqlite3.node
 → /home/me/.cache/deno/npm/registry.npmjs.org/better-sqlite3/11.3.0/Debug/better_sqlite3.node
 → /home/me/.cache/deno/npm/registry.npmjs.org/better-sqlite3/11.3.0/out/Release/better_sqlite3.node
 → /home/me/.cache/deno/npm/registry.npmjs.org/better-sqlite3/11.3.0/Release/better_sqlite3.node
 → /home/me/.cache/deno/npm/registry.npmjs.org/better-sqlite3/11.3.0/build/default/better_sqlite3.node
 → /home/me/.cache/deno/npm/registry.npmjs.org/better-sqlite3/11.3.0/compiled/20.11.1/linux/x64/better_sqlite3.node
 → /home/me/.cache/deno/npm/registry.npmjs.org/better-sqlite3/11.3.0/addon-build/release/install-root/better_sqlite3.node
 → /home/me/.cache/deno/npm/registry.npmjs.org/better-sqlite3/11.3.0/addon-build/debug/install-root/better_sqlite3.node
 → /home/me/.cache/deno/npm/registry.npmjs.org/better-sqlite3/11.3.0/addon-build/default/install-root/better_sqlite3.node
 → /home/me/.cache/deno/npm/registry.npmjs.org/better-sqlite3/11.3.0/lib/binding/node-v108-linux-x64/better_sqlite3.node
    at bindings (file:///home/me/.cache/deno/npm/registry.npmjs.org/bindings/1.5.0/bindings.js:126:9)
    at new Database (file:///home/me/.cache/deno/npm/registry.npmjs.org/better-sqlite3/11.3.0/lib/database.js:48:64)
    at file:///home/me/$deno$eval.ts:1:44
dector commented 1 month ago

Might be dupe for #15717

dector commented 1 month ago

It's interesting! Libsql totally works!

$ deno eval "import Database from 'npm:libsql'; const db = new Database(':memory:'); console.log('Wow!')"
Wow!
nathanwhit commented 1 month ago

The underlying reason is that better-sqlite3 uses internal node C++ APIs directly. Those are not stable across node versions (for instance, better-sqlite3 needs a different build for each node version, and internal changes in node can break it) and for this reason they aren't supported by deno.

If better-sqlite3 were updated to use NAPI, which is a stable interface for node native addons, instead of node's internal APIs it would work on deno.


Other packages (such as sqlite3 and libsql) do use NAPI, and so they work in deno

dector commented 1 month ago

Thanks for the explanation!

I dunno how important part of the ecosystem is better-sqlite3 but libsql is quite awesome.

Feel free to close this issue as "wan't fix".

littledivy commented 1 month ago

It is technically possible to emulate Node.js NaN and expose V8 C++ symbols. There are usually not any breaking changes to old V8 APIs that modules like better-sqlite3 might be using.

teddybee commented 1 week ago

Same issue here :( deno eval "import Database from 'npm:better-sqlite3'; const db = new Database(':memory:'); console.log('Wow!')"

Warning experimentalDecorators compiler option is deprecated and may be removed at any time
error: Uncaught (in promise) TypeError: LoadLibraryExW failed: A dynamic link library (DLL) initialization routine failed. (os error 1114)
    at Object.Module._extensions..node (node:module:807:20)
    at Module.load (node:module:662:32)
    at Function.Module._load (node:module:534:12)
    at Module.require (node:module:681:19)
    at require (node:module:818:16)
    at bindings (file:///D:/NodeJS/***/***/node_modules/.deno/bindings@1.5.0/node_modules/bindings/bindings.js:112:48)
    at new Database (file:///D:/NodeJS/***/***/node_modules/.deno/better-sqlite3@11.5.0/node_modules/better-sqlite3/lib/database.js:48:64)
    at file:///D:/NodeJS/***/***/apps/backend/$deno$eval.ts:1:55