Generate Anki flashcards with Node.js
Use Node.js 22.13 or later. In a new application directory:
mkdir anki-deckcd anki-decknpm install --include=optional ankiforge@0.3.1This installs the public npm package and the matching native optional dependency. See verified environments for platform coverage.
Export a deck
Section titled “Export a deck”Save this as main.mjs:
import { Project, Note, BuildOptions } from 'ankiforge';
const project = new Project('spanish').defaultDeck('Spanish');project.add('es:hola', Note.basic('hola', 'hello'));const output = await project.build(BuildOptions.to('spanish.apkg'));console.log(output.artifact.path);await output.artifact.close();node main.mjsOpen the persistent spanish.apkg in Anki. It contains hola → hello in Spanish.
Keep namespace spanish and key es:hola stable for later edits. Closing the persistent artifact leaves the file on disk.
Exporting does not require Anki to be installed.
TypeScript and next tasks
Section titled “TypeScript and next tasks”The JSON to Anki recipe includes a complete TypeScript compile-and-run path.
Use .mjs for ESM, or require('ankiforge') for CommonJS. TypeScript declarations ship with the package;
see TypeScript configuration.
- Images and audio and Node media API
- Custom note types and template bundles
- Image Occlusion
- Compare and update
- Node API and troubleshooting
Source builds and local packaging are in source builds.