Skip to content

Generate Anki flashcards with Node.js

Use Node.js 22.13 or later. In a new application directory:

Terminal window
mkdir anki-deck
cd anki-deck
npm install --include=optional ankiforge@0.3.1

This installs the public npm package and the matching native optional dependency. See verified environments for platform coverage.

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();
Terminal window
node main.mjs

Open 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.

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.

Source builds and local packaging are in source builds.

View Markdown