Style Dictionary
Style Dictionary is a powerful build system for creating and managing design tokens across multiple platforms and technologies. It allows developers and designers to define styles once using a single source of truth, typically JSON-based design tokens, and then automatically generate platform-specific assets such as CSS variables, Sass maps, iOS `.h`/`.m` files, Android XML resources, and JavaScript objects. The current stable version is 5.4.0, with frequent patch and minor releases, often adding support for the latest Design Token Community Group (DTCG) specification drafts. Its key differentiator is its highly configurable transformation and formatting pipeline, enabling extensive customization for diverse output requirements across web, iOS, and Android ecosystems. It's designed to streamline design system implementation by ensuring consistency and reducing manual synchronization efforts, thereby solving errors, roadblocks, and workflow inefficiencies.
Common errors
-
Error: Node.js version x.y.z is not supported. Required: >=22.0.0.
cause The installed Node.js version does not meet Style Dictionary's minimum requirement.fixUpgrade your Node.js installation to version 22.0.0 or newer. Use a tool like `nvm` to manage Node.js versions: `nvm install 22 && nvm use 22`. -
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".js" for /path/to/build.js
cause Attempting to use ES module `import` syntax in a CommonJS context without proper configuration.fixEnsure your `package.json` contains `"type": "module"` if you are using `.js` files with `import`/`export`. Alternatively, rename your build script to `build.mjs` or dynamically import Style Dictionary: `const StyleDictionary = (await import('style-dictionary')).default;`. -
Reference Errors: Some token references (X) could not be found.
cause Design tokens are referencing non-existent paths, tokens that have been filtered out, or incorrectly structured values.fixReview your token files and configuration. Ensure all references (`{...}`) point to valid, defined design token paths. Check that no filters are inadvertently excluding referenced tokens. Pay close attention to nested namespaces, especially those with special characters or if exporting from tools like Figma Tokens Studio. -
StyleDictionary.extend is not a function
cause Attempting to call `StyleDictionary.extend` on an already instantiated object, or using an incorrect import/instantiation pattern from v3 to v4/v5.fixSince v4, `StyleDictionary` is a class. You should use `await StyleDictionary.extend(config)` or `const sd = new StyleDictionary(config); await sd.init();` to create an instance. Ensure `await` is used as `extend` is now asynchronous.
Warnings
- breaking Style Dictionary v4 introduced significant breaking changes, including a complete rewrite to ES Modules, making `StyleDictionary.extend()` and build methods asynchronous, and changes to the API for registering custom transforms and formats. Codemods are available to assist with migration.
- breaking As of Style Dictionary v5.0.0, the minimum required Node.js version is 22.0.0. This is primarily to leverage `Set.prototype.union` for performance improvements in token reference resolution.
- breaking References to non-token leaf nodes or with the `.value` suffix are no longer supported. The reference syntax (e.g., `{ref.foo}`) is now strictly aligned with the DTCG spec and cannot be customized via options.
- gotcha Style Dictionary is actively adopting the Design Token Community Group (DTCG) specification. Recent versions (v5.3.x, v5.4.x) added support for structured color and dimension token formats (DTCG v2025.10). While largely backwards compatible, fully leveraging these new formats might require adjusting existing token definitions or custom transforms.
- gotcha Older v5.x versions (pre-5.0.4) had an issue with overly eager token collision warnings. While fixed in 5.0.4, users on earlier patches might encounter excessive warnings for identical token values.
- gotcha A regression bug in `sizeRem` transform could cause errors for NaN values in v5.1.4. Also, issues with `outputReferences` for tokens containing `.value` in their name (pre-v5.1.1) and `fontName` parsing (pre-v5.1.0) have been patched.
Install
-
npm install style-dictionary -
yarn add style-dictionary -
pnpm add style-dictionary
Imports
- StyleDictionary
const StyleDictionary = require('style-dictionary');import StyleDictionary from 'style-dictionary';
- registerTransform, registerFormat, registerFilter
import { registerTransform } from 'style-dictionary';import StyleDictionary from 'style-dictionary'; StyleDictionary.registerTransform(...);
- Config, DesignTokens, Transform
import type { Config } from 'style-dictionary';import type { Config, DesignTokens, Transform } from 'style-dictionary/types';
Quickstart
import StyleDictionary from 'style-dictionary';
import fs from 'fs';
import path from 'path';
// 1. Define your design tokens (e.g., in tokens/colors.json)
const tokens = {
"color": {
"brand": {
"primary": { "value": "#007bff" },
"secondary": { "value": "#6c757d" }
}
},
"size": {
"font": {
"base": { "value": "16px" },
"large": { "value": "24px" }
}
}
};
// Create a tokens directory and write the tokens file if it doesn't exist
const tokensDir = path.resolve(process.cwd(), 'tokens');
const tokensFilePath = path.join(tokensDir, 'colors.json');
if (!fs.existsSync(tokensDir)) {
fs.mkdirSync(tokensDir, { recursive: true });
}
fs.writeFileSync(tokensFilePath, JSON.stringify(tokens, null, 2));
// 2. Define your Style Dictionary configuration
const config = {
source: ['tokens/**/*.json'],
platforms: {
css: {
transformGroup: 'css',
buildPath: 'build/',
files: [
{
destination: '_variables.css',
format: 'css/variables'
}
]
},
js: {
transformGroup: 'js/es6',
buildPath: 'build/',
files: [
{
destination: 'tokens.js',
format: 'javascript/es6'
}
]
}
}
};
// Create a build directory if it doesn't exist
const outputDir = path.resolve(process.cwd(), 'build');
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// 3. Extend Style Dictionary and build all platforms
async function buildTokens() {
// StyleDictionary.extend is now asynchronous since v4
const sd = await StyleDictionary.extend(config);
sd.buildAllPlatforms();
console.log('Style Dictionary build completed successfully!');
// To clean generated files:
// sd.cleanAllPlatforms();
}
buildTokens().catch(err => {
console.error('Style Dictionary build failed:', err);
process.exit(1);
});