Skip to content

Migrating from v5 to v6 โ€‹

Version 6 is a rewrite of RPG Dice Roller. Most familiar dice notation still works, but the JavaScript API and saved data format have changed.

This guide focuses on the changes you need to make to an existing application. For an overview of the new functionality, see What's new in v6.

Before upgrading โ€‹

  • Check that your environment supports v6. The package requires Node.js 22 or later, or a modern browser. See Environment support.
  • Check the compatibility of any Dice Roller integrations you use, including the Vue components, Markdown plugins and CLI. Use versions that explicitly support v6.
  • Update the @dice-roller/rpg-dice-roller dependency to v6. The package name is unchanged, and ES modules, CommonJS and UMD are still supported.
  • If you load a bundle directly, update paths from lib/ to dist/. See Get started for the supported imports and browser setup. TypeScript definitions are included.

Saved v5 rolls are incompatible with v6

Version 6 cannot directly import rolls or histories exported by v5.

If your application saves rolls in a database, local storage or files, plan how to migrate or retain that data before upgrading. Simply changing the library version is not enough.

See Migrating saved data below.

What's changed? โ€‹

The main changes affecting existing applications are:

  • Breaking changes: renamed classes and namespaces, a new result tree and history API, separate presenters, new configuration and incompatible saved data.
  • New features: programmatic expressions, custom dice, weighted targets, nested dice calculations, variables, conditionals, the Map modifier, repeating modifier enhancements and external dice sources.
  • Fixes and behaviour changes: improved validation and syntax errors, more accurate roll statistics and configurable safety limits.

See What's new in v6 for the full overview.

Quick reference โ€‹

The following table maps common v5 API entry points to their v6 equivalents. Some are replacements rather than direct renames.

v5v6
DiceRollRoll
DiceRollerRoller
new DiceRoll(notation)new Roll(notation) or roll(notation)
diceRoll.roll()roll.reroll() (returns a new roll)
diceRoller.roll(...notations)roller.roll(notation), once per expression
diceRoller.logroller.history.rolls
diceRoller.totalroller.history.total
diceRoller.clearLog()roller.history.clear()
diceRoll.rollsroll.result (a tree, not an array)
diceRoll.hasExpressions() / hasRolls()No direct equivalent; inspect the expression or result tree
diceRoll.output / diceRoller.outputroll.present() / roller.present()
notation, total, minTotal, maxTotal, averageTotal on a rollSame property names; calculated statistics can now be null
DiceRoll.import(data)Roll.import(data) (v6 data only)
DiceRoller.import(data) / new DiceRoller(data)new Roller() followed by roller.import(data)
diceRoller.import(data)roller.import(data) (returns the newly imported rolls)
exportFormats.JSON / BASE_64 / OBJECT'json' / 'base64' / 'object'
Parser.parse(notation)new Parser().parse(notation) or roller.parse(notation)
ComparePointast.compare()
RollGroupast.group() and group results
Dicedice (die definitions and registries)
Modifiersmodifiers (handlers and registries), with builders in ast
Resultsresults (result types and helpers)
Exceptionserrors (different error classes)
NumberGenerator.generatorroller.generator
NumberGenerator.enginesengine option, EngineRegistry and engines namespace

The API reference contains the complete v6 API.

Updating your code โ€‹

Imports and roll creation โ€‹

The two main classes have been renamed. Their basic usage is similar.

javascript
import { DiceRoll, DiceRoller } from '@dice-roller/rpg-dice-roller';

const result = new DiceRoll('2d6+3');

const roller = new DiceRoller();
const attack = roller.roll('d20+5');

Update the names in your imports regardless of whether you use ES modules, CommonJS or the browser global.

Unlike v5, roller.roll() accepts only one notation string or expression node. Its optional second argument is an options object, not another notation.

If you currently roll several notations in one call, call the method separately for each one:

javascript
const rolls = roller.roll('d20', '2d6+3');

Ordinary rolls are still synchronous. You don't need to introduce await throughout your application. It is only needed if you choose to use an asynchronous external dice source.

Re-rolling โ€‹

In v5, calling roll() on a DiceRoll replaces its existing results and returns the new results array. In v6, reroll() creates a new Roll, leaving the original unchanged.

javascript
const result = new DiceRoll('2d6');

result.roll();
console.log(result.total);

Make sure your application uses the returned roll. Calling original.reroll() without retaining its return value doesn't update original.

If the original roll belongs to a history, re-rolling it doesn't replace the original entry or automatically append a new one. Use roller.history.add(result) to record the new roll, or roller.roll(notation) to make and record a new roll directly.

See Rolling dice for more about re-rolling and roll creation.

Roll history โ€‹

The history is now a separate RollHistory object.

javascript
const roller = new DiceRoller();

roller.roll('d20', '2d6');

console.log(roller.log.length);
console.log(roller.total);

for (const result of roller.log) {
  console.log(result.total);
}

roller.clearLog();

In v5, you could manipulate the array returned by log directly. In v6, history.rolls returns a shallow copy of the list. Use history.add() and history.clear() to modify the history, rather than mutating that array.

A standalone RollHistory is also available if you want to manage results without using a Roller.

See Roll history.

Accessing dice results โ€‹

The old RollResult, RollResults and ResultGroup classes have been replaced by plain, readonly result nodes. The result tree follows the structure of the evaluated expression.

For a simple notation containing a single dice pool:

javascript
const result = new DiceRoll('4d6kh3');
const pool = result.rolls[0];

console.log(pool.rolls.map(die => die.value));

For more complex expressions, the root may instead represent a mathematical operation, group, comparison or another expression type. Don't assume that result.result always has a rolls property. Follow the child nodes to find the pools.

The individual dice still expose these familiar properties:

PropertyMeaning in v6
initialValueOriginal value of the result before modification.
valueValue after modifiers.
calculationValueValue used for scoring.
useInTotalWhether the score contributes to the total.
flagsArray of semantic modifier flags, replacing the old modifiers and modifierFlags accessors.

For example, replace a check of die.modifiers.has('drop') with die.flags.includes('drop'). To determine whether a die contributes to the total, use useInTotal rather than checking its flags or summing its face value.

If your application displays individual dice, each pool now provides a snapshot of its resolved die type and properties through pool.die.

See Working with results, including an example of traversing an arbitrary expression to find all of its dice pools.

Displaying rolls โ€‹

Replace the output property with present():

javascript
console.log(result.output);
console.log(roller.output);

toString() and string interpolation continue to produce text, using the default text presenter. If you configure a different presenter, use present() to obtain its output; that output needn't be a string.

If your application parses formatted output to identify dice, modifier effects or totals, migrate it to the result tree instead. The default formatting is not identical to v5, and presentations can be customised.

See Presenters.

Importing and exporting โ€‹

The same three formats are supported, but format identifiers are now strings instead of the numeric exportFormats constants.

javascript
import { DiceRoll, DiceRoller, exportFormats } from '@dice-roller/rpg-dice-roller';

const result = new DiceRoll('2d6');
const saved = result.export(exportFormats.JSON);
const restored = DiceRoll.import(saved);

Default export format

In v5, export() returns a JSON string by default. In v6, it returns a JSON-safe object.

Use export('json') wherever your existing code expects a string, or update the surrounding storage and transport code to accept an object.

History imports no longer use a static method or constructor argument:

javascript
const restored = DiceRoller.import(savedHistory);

Each example assumes savedHistory was created by the matching major version. In v6, the import method returns an array of the newly imported rolls and adds them to the existing history. An invalid history import leaves the history unchanged.

The versioned v6 data has type: 'roll' or type: 'history' and version: 1. History entries are in rolls instead of log; an individual roll has a result tree instead of its old rolls array. Formatted output isn't stored.

See Importing and exporting for the formats and structure.

Migrating saved data โ€‹

Decide whether historical results must be preserved:

  • If they aren't needed, retain the notation and generate new rolls in v6 when appropriate. These are new outcomes, not the original results.
  • If you only need to display historical records, retain them separately in their old format, or store the notation, total and display text independently.
  • If historical rolls need to become v6 Roll objects or histories, use the data converter to migrate your saved v5 exports without rolling the dice again.

You can convert individual exports using the online converter, or install the converter package to migrate data programmatically:

javascript
import { convert } from '@dice-roller/convertor';
import { Roll, Roller } from '@dice-roller/rpg-dice-roller';

// savedData is an existing v5 export.
const converted = convert(savedData, 1);

if (converted.type === 'roll') {
  const restored = Roll.import(converted);
  console.log(restored.total);
} else {
  const roller = new Roller();
  roller.import(converted);
}

Use the full format to preserve the original results and historical statistics. Back up the original data and test representative saved rolls, including modified and grouped results, before replacing them.

The converter rejects inconsistent or ambiguous results by default. See Best-effort conversion if you need to handle recoverable issues.

If you don't have a complete export, but know the notation and finished dice values, you can also reconstruct individual rolls using the concise import format. Don't pass v5 result arrays directly to Roll.import().

Parsing and expressions โ€‹

The v5 parser returns an array of dice and other parsed values. The v6 parser returns an abstract syntax tree (AST), describing the expression without rolling it.

javascript
const expression = Parser.parse('2d6+3');

If you use a Roller, you can instead call roller.parse() to use its configured parser.

If you previously constructed dice, modifiers or groups as class instances, use the AST builders to compose expressions. For example, new ComparePoint('>=', 5) becomes ast.compare('>=', 5) when specifying a modifier's comparison.

See Parsing and building expressions.

Random number generation โ€‹

The v5 global number generator has been replaced by configuration on individual rolls and rollers. The library uses pure-rand instead of random-js.

javascript
import { DiceRoller, NumberGenerator } from '@dice-roller/rpg-dice-roller';

NumberGenerator.generator.engine =
  NumberGenerator.engines.MersenneTwister19937.seed(1234);

const roller = new DiceRoller();
v5 enginev6 engine option
nativeMath'nativeMath'
MersenneTwister19937'mersenne'
browserCrypto or nodeCrypto'crypto'
min and maxNo equivalent engine option; use calculated totals or external die draws as appropriate

V6 also supports the seedable xoroshiro128plus engine. Engine names and seeding behaviour have changed; don't rely on v6 reproducing v5 seeded sequences. Update deterministic tests accordingly.

Custom engines still implement next(), but must supply uniformly distributed signed 32-bit integers. Revisit any engine that relies on the v5 generator setting its range property, such as fixed-result engines. For exact die faces, use the new drawDie option instead.

To generate random numbers directly, use roller.generator.integer() or roller.generator.real(). These share the roller's random sequence.

See Random number generator.

Custom dice and modifiers โ€‹

The Dice and Modifiers class hierarchies have been replaced by definitions and handlers. For ordinary notation, you don't need to register anything: the built-in dice and modifiers are available automatically.

If you customised dice in v5, use the dice namespace:

v5 classv6 definition
Dice.StandardDicedice.standardDie(sides)
Dice.PercentileDicedice.percentileDie
Dice.FudgeDicedice.fudgeDie(nonBlanks)

These are definitions, not rollable dice instances. Quantities and modifiers belong to expression nodes, and custom definitions are made available through a dice.DieRegistry.

If you changed modifier order globally, move that configuration to a registry:

javascript
import { DiceRoller, Modifiers } from '@dice-roller/rpg-dice-roller';

Modifiers.KeepModifier.order = 2;

const roller = new DiceRoller();

The v6 configuration only applies to rolls using that registry. Recheck custom order values: v6 introduces Matching-batch explode before Explode and Map before Keep and Drop, changing the default numeric order of later handlers. See the modifier execution order for the current defaults.

To migrate a custom modifier implementation, write a handler rather than extending the v5 Modifier or ComparisonModifier classes. Built-in modifier AST nodes can be created with builders such as ast.explode(), ast.keep() and ast.target().

See Custom dice, Modifiers and Configuring rolls.

Handling errors โ€‹

The Exceptions namespace is now errors, and the old exception classes don't all have one-to-one replacements.

v5 errorWhat to catch in v6
Parser syntax errors / Exceptions.NotationErrorerrors.NotationError for invalid syntax
Exceptions.RequiredArgumentErrorerrors.ValidationError for missing or invalid arguments
Exceptions.CompareOperatorError / DieActionValueErrorerrors.ValidationError or errors.EvaluationError, depending on the failure
Exceptions.DataFormatErrorerrors.ImportError for malformed imported data

Instead of relying on old error messages or exact exception names, handle errors according to the operation being performed. All library-defined errors extend errors.DiceRollerError, although other exceptions can still propagate from your own code or dependencies.

javascript
import { Roll, errors } from '@dice-roller/rpg-dice-roller';

try {
  const result = new Roll('2d6+');
} catch (error) {
  if (error instanceof errors.NotationError) {
    console.error(error.format());
  } else {
    throw error;
  }
}

See Handling errors for syntax locations, validation and asynchronous failures.

Notation and behaviour changes โ€‹

Most existing notation continues to work, but review expressions that depend on the following behaviour.

Dice quantities and validation โ€‹

V5 truncates fractional computed dice quantities. For example, (3/2)d6 rolls one die. V6 rejects the fractional quantity.

Make your intended rounding explicit:

v5v6 replacement
(3/2)d6(floor(3/2))d6

Computed quantities can now evaluate to zero, in which case no dice are rolled for that pool. Literal 0d6 remains invalid.

The default maximum initial quantity is 999, configurable through limits.maxQty. Literal quantities are checked during parsing, and calculated quantities during evaluation.

Target success and failure โ€‹

V5 checks the success condition before the failure condition. V6 allows multiple success and failure rules and uses the last matching rule in written order. Overlapping conditions can therefore produce different totals.

For example, with 4d6>=4f=6, a six counts as a success in v5 but a failure in v6. If you intended the original success to take precedence, use 4d6>=4f=6>=4 instead, or simplify the overlapping conditions.

Success and failure rules can also be weighted with :amount, where the amount must be a whole number from 1 to 10 and defaults to 1.

Write all target rules together. For example, 4d6>=4f=1kh3 is valid, but 4d6>=4kh3f=1 is not.

See Target success and Target failure.

Critical success and failure โ€‹

The default text presenter uses cs and cf for critical success and failure, replacing v5's ** and __.

Repeated * and _ markers now represent weighted target successes and failures. Update any tests or integrations that rely on the old text symbols. If you need to retain a particular appearance, configure the Text presenter.

Repeating modifiers โ€‹

Explode, Re-roll and Unique now accept explicit iteration limits, such as d6!3, d6r2 and 4d6u3. The existing ro and uo aliases still work, corresponding to one replacement generation or retry.

The configurable maxIterations limit caps repeating modifiers, and maxGeneratedRolls caps additional draws across repeating modifiers in each pool. Exhausting these limits stops additional draws rather than throwing just for reaching the limit.

If you've customised repeating behaviour or rely on exact sequences of random draws, test those expressions with the new evaluator.

See Repeating modifier limits and Roll limits.

Custom faces and ranges โ€‹

V6 introduces d{1,1,4} for explicit faces, d{-2...3} for a range and d{0:0.5:2} for a stepped range. These are new forms, not required replacements for standard dice.

Review any code that generates or validates notation independently of the library, especially code that assumes every use of braces denotes a roll group or that all die faces are positive integers.

See Dice notation for the syntax and limits.

Mathematical operations โ€‹

V6 no longer depends on MathJS. Most mathematical operations now follow native JavaScript number semantics; floored modulo is retained. This can change results at floating-point boundaries and for some special cases:

Expressionv6 behaviour
round(-2.5)-2 (halfway values round towards positive infinity).
floor(2.9999999999999996)2 (no tolerance-based snapping).
ceil((0.1+0.2)*10)4 (native floating-point arithmetic).
min(1.0000000000001, 1)1 (strict numerical comparison).
pow(-8, 1/3)Throws an EvaluationError (native powers do not support this real root).
5%0Throws an EvaluationError.

Check any expressions relying on the previous rounding, power or modulo-by-zero behaviour, especially calculated dice quantities and sides. See Maths.

Totals and calculations โ€‹

The familiar total, minTotal, maxTotal and averageTotal properties remain, but the calculated statistics now have the type number | null.

  • Minimum and maximum totals account for supported arithmetic and modifiers. For example, 10-d6 now has a minimum of 4 and a maximum of 9.
  • The average now accounts for the probabilities of possible results for supported expressions, including standalone Keep and Drop modifiers. V6 no longer falls back to the midpoint.
  • Unsupported statistics return null. Check for this before formatting, comparing or performing arithmetic on them. Full exports preserve these values.
  • Statistics are calculated lazily without rolling again. They model default built-in dice and modifier behaviour, even when these have been customised.
  • Modifier execution during an actual roll still follows the configured order rather than the written notation order.
javascript
console.log(roll.averageTotal ?? 'Unavailable');

See Calculated totals for the precise behaviour and limitations.

Taking advantage of v6 โ€‹

You don't need to adopt every new feature just to upgrade. Once your application is working, you can:

  • Build reusable expressions without assembling notation strings using the AST builders.
  • Configure game-specific dice and modifiers without changing the rules for every roll in your application.
  • Use presenters for graphical or customised output.
  • Supply application data through variables, or integrate a physical or graphical roller using external dice sources.

Migration checklist โ€‹

  • [ ] Upgrade the library and check environment and integration compatibility.
  • [ ] Update imports, constructors, roll calls and re-roll handling.
  • [ ] Move history access and modification to RollHistory.
  • [ ] Adapt result inspection and output formatting.
  • [ ] Back up and migrate or retain v5 saved data, and specify the desired export format.
  • [ ] Update RNG configuration, custom dice, modifiers and error handling.
  • [ ] Handle nullable roll statistics in application code and saved data.
  • [ ] Review notation and tests that rely on changed validation, scoring, output or deterministic random sequences.
  • [ ] Test the upgraded application, including restoring saved rolls and evaluating representative game mechanics.

For further information, see the usage guide and API reference.

MIT Licensed ยท Credits