# genanki-js

> A JavaScript implementation for generating Anki decks in browser client side

Latest version **2.1.0** (published 2026-11-19) · AGPL-3.0-or-later license · 0 weekly downloads

## Install

```sh
npm install genanki-js
pnpm add genanki-js
yarn add genanki-js
bun add genanki-js
```

## Health

**Score 60/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2026-11-19 |
| First published | 2022-05-03 |
| Weekly downloads | 0 |
| License | AGPL-3.0-or-later |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 169 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 80 |
| Author | Mani |
| Maintainers | krmanik |
| Keywords | genanki, anki, apkg, flashcards |

## Links

- npm: https://www.npmjs.com/package/genanki-js
- Repository: https://github.com/krmanik/genanki-js
- Homepage: https://github.com/krmanik/genanki-js#readme
- Issues: https://github.com/krmanik/genanki-js/issues
- npm.io page: https://npm.io/package/genanki-js

## Dependencies (2)

- [jszip](https://npm.io/package/jszip.md) ^3.10.1
- [sql.js](https://npm.io/package/sql.js.md) ^1.15.0

## Recent versions

- 2.1.0 (latest) — 2026-11-19
- 2.0.0 — 2022-05-03

## README

# genanki-js
A JavaScript implementation for generating Anki decks in browser. This is fork of [mkanki](https://github.com/nornagon/mkanki).

# QuickStart
genanki-js can be used from npm (with a bundler such as Vite, or in Node.js), or with a plain `<script>` tag.

## npm
```
npm install genanki-js
```

```js
import { Model, Deck, Package } from "genanki-js"
// or: const { Model, Deck, Package } = require("genanki-js")
```

[JSZip](https://github.com/Stuk/jszip) and [sql.js](https://github.com/sql-js/sql.js) are installed as dependencies and loaded automatically, nothing has to be configured. sql.js is only loaded when the first package is generated.

- **Browser**: `package.writeToFile('deck.apkg')` downloads the file.
- **Node.js**: use `package.generate('nodebuffer')` and write the result yourself:
  ```js
  import { writeFile } from "node:fs/promises"
  await writeFile("deck.apkg", await p.generate("nodebuffer"))
  ```

<details>
<summary>esbuild and webpack configuration</summary>

sql.js contains `require("node:fs")` and `require("node:crypto")` calls that only run in Node.js. Vite handles this automatically. For esbuild and webpack, mark them as external:

```sh
esbuild src/main.js --bundle --external:node:*
```

```js
// webpack.config.js
module.exports = {
  // ...
  externals: { "node:fs": "{}", "node:crypto": "{}" },
}
```
</details>

To use another sql.js build (for example `sql-wasm.js` with your own `locateFile`), pass it with `setSqlJs`:
```js
import initSqlJs from "sql.js"
import { setSqlJs } from "genanki-js"

setSqlJs(await initSqlJs({ locateFile: file => `/assets/${file}` }))
```

## Script tag
Download the [genanki](https://github.com/krmanik/genanki-js/releases) zip file from the release page. The zip file contains two folders

- `dist` : This folder contains `genanki.js` file. If project already configured with [sql.js](https://github.com/sql-js/sql.js) and [JSZip](https://github.com/Stuk/jszip) then just add `genanki.js` file to project.

- `sample` : This folder contains [sql.js](https://github.com/sql-js/sql.js), [FileSaver.js](https://github.com/eligrey/FileSaver.js), [JSZip](https://github.com/Stuk/jszip) and [genanki.js](https://github.com/krmanik/genanki-js). It is ready to use folder.

Alternatively, `genanki.js` can also be loaded from CDN.
```html
<script src="https://cdn.jsdelivr.net/npm/genanki-js@2.1.0/dist/genanki.js"></script>
```

### Set up a new project from scratch
1. Add [sql.js](https://github.com/sql-js/sql.js) and [JSZip](https://github.com/Stuk/jszip) to the project, then `genanki.js` from the `dist` folder. [FileSaver.js](https://github.com/eligrey/FileSaver.js) is optional, it is used for downloading when available.

    >*Note: [mkanki](https://github.com/nornagon/mkanki) uses `better-sql`, `fs` and `archiver`, that make it difficult to be used in browser*

```html
<!-- sqlite, sql-wasm-inline.js from sql.js renamed to sql.js -->
<script src='js/sql/sql.js'></script>

<!-- jszip for .apkg -->
<script src="js/jszip.min.js"></script>

<!-- optional, File saver -->
<script src="js/filesaver/FileSaver.min.js"></script>

<!-- for creating and exporting anki package file -->
<script src='genanki.js'></script>
```

2. genanki.js calls the global `initSqlJs()` when the first package is generated. If sql.js needs a configuration, for example the location of the `.wasm` file, initialize it yourself and pass it to genanki.js
```js
initSqlJs({
    locateFile: filename => `js/sql/${filename}`
}).then(function (SQL) {
    setSqlJs(SQL);
});
```

3. Now use following `Examples` to generate and export decks.

The classes are available as globals (`Model`, `Deck`, `Package`, ...) and in the `genanki` namespace (`genanki.Model`, ...).

# Documentation
View [Documentation](https://krmanik.github.io/genanki-js/#/)

*View more examples here [Examples](https://krmanik.github.io/genanki-js/#/examples)*

# CSV/TSV to Anki Package
Visit page and select `CSV to APKG` from top menu.<br>
[CSV to APKG](https://krmanik.github.io/genanki-js/demo/index.html#)

# Examples
```js
var m = new Model({
  name: "Basic (and reversed card)",
  id: "1543634829843",
  flds: [
    { name: "Front" },
    { name: "Back" }
  ],
  req: [
    [ 0, "all", [ 0 ] ],
    [ 1, "all", [ 1 ] ]
  ],
  tmpls: [
    {
      name: "Card 1",
      qfmt: "{{Front}}",
      afmt: "{{FrontSide}}\n\n<hr id=answer>\n\n{{Back}}",
    },
    {
      name: "Card 2",
      qfmt: "{{Back}}",
      afmt: "{{FrontSide}}\n\n<hr id=answer>\n\n{{Front}}",
    }
  ],
})
                        
var d = new Deck(1276438724672, "Test Deck")

d.addNote(m.note(['this is front', 'this is back']))

var p = new Package()
p.addDeck(d)

p.writeToFile('deck.apkg')
```

### Cloze
```js
var m = new ClozeModel({
  name: "Cloze",
  id: "1550428389000",
  flds: [
    { name: "Text" },
    { name: "Back Extra" }
  ],
  tmpl: {
    qfmt: "{{cloze:Text}}",
    afmt: "{{cloze:Text}}<br>{{Back Extra}}",
  },
})

var d = new Deck(1550428389001, "Cloze Deck")
d.addNote(m.note(['{{c1::Paris}} is the capital of {{c2::France}}', 'Europe']))

var p = new Package()
p.addDeck(d)
p.writeToFile('cloze-deck.apkg')
```

# Development
```
npm install
npm run build   # bundle genanki/*.js to dist/ (and the demo and sample folders)
npm test
npm run vendor  # update sql.js, JSZip and FileSaver in the demo and sample folders
```

# License
### [genanki-js](https://github.com/krmanik/genanki-js)
[GNU Affero General Public License v3](https://opensource.org/licenses/AGPL-3.0)
<br>Copyright (c) 2021 Mani

## Other Third Party Licenses
[License.md](https://github.com/krmanik/genanki-js/blob/main/License.md)

---
_Source: https://npm.io/package/genanki-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
