npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@allnulled/sqlite-polyfill

v1.0.1

Published

SQLite Polyfill. Cross-environment: same API for Node.js and browser.

Readme

sqlite-polyfill

Use sqlite3 on Node.js and sql.js on browser, through the same API.

Installation

npm install -s @allnulled/sqlite-polyfill

Load

First, if you have doubts, take a look to the test/test.js file, which works on both environments, but see also test/index.html for the browser implementation. You'll see this explanation in action.

So, despite the API is the same for both environments, there are some differences you must take care of on the load step.

In Node.js, with this conditional you can patch on browser, and set to global scope on node.js:

if(typeof global !== "undefined") {
    require(__dirname + "/../sqlite-polyfill.js");
}

In browser, though, you have to load the polyfill as script tag, and the sql.js with its wasm file like so:

<script src="sqljs-wasm/sql-wasm.js"></script>
<script src="sqlite-polyfill/sqlite-polyfill.js"></script>

Then, you will have to copy the following files and integrate them in your HTML, in this same order:

  • test/sqljs-wasm/sql-wasm.js
  • test/sqljs-wasm/sql-wasm.wasm (not included by script, but by API init method)
  • test/sqlite-polyfill/sqlite-polyfill.js

Usage

The last conflictive point is the init function, which takes parameters for both environments.

const db = new SQLitePolyfill();
// @ATTENTION!
//   - In browser, 1st parameter is the key in localStorage
//   - In node.js, 2nd parameter is ignored. See this path is relative to where you put the `test/sqljs-wasm/sql-wasm.wasm`
await db.init('example.db', "sqljs-wasm/$filename");

await db.run("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, name TEXT)");
await db.run("INSERT INTO test (name) VALUES (?)", ["Alice"]);

const rows = await db.all("SELECT * FROM test");
console.log(rows);

await db.close();

Use cases

This library can be useful to create (1) cross-environment (2) database-oriented scripts (3) based on SQL/SQLite.

Extra features

The polyfill comes with some extra advantages that the libraries may not come. For example:

  • Persistence for both
    • patched using localStorage in browser
    • the localStorage ID is directly the same name of the file. For now I don't see any problem about this.

Test

This is an example of how to use it in node.js and browser with the same script:

(async () => {
  if(typeof global !== "undefined") {
    require(__dirname + "/../sqlite-polyfill.js");
  }
  const db = new SQLitePolyfill();
  // @ATTENTION!
  //   - In browser, 1st parameter is the key in localStorage
  //   - In node.js, 2nd parameter is ignored. See this path is relative to where you put the `test/sqljs-wasm/sql-wasm.wasm`
  await db.init('example.db', "sqljs-wasm/$filename");

  await db.run("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, name TEXT)");
  await db.run("INSERT INTO test (name) VALUES (?)", ["Alice"]);

  const rows = await db.all("SELECT * FROM test");
  console.log(rows);

  await db.close();
})();

Yes, the same than before. I think it is enough clear. The test is imported from node and from browser, and works for both.

Yes, sqlite. With the WASM bindings and persistence for browser.