Storyblok CLI
The Storyblok CLI (Command Line Interface) is a powerful development tool designed to enhance the developer experience for projects integrating with the Storyblok Headless CMS. It provides a robust set of features for managing Storyblok spaces directly from the terminal, including generating TypeScript type definitions for components to ensure type safety in frontend applications. The current stable version is 4.16.9, released very recently, indicating an active development and release cadence, often as part of a monorepo that includes various Storyblok SDKs. Key differentiators include its comprehensive component management capabilities (pulling, pushing schemas, groups, presets, tags), a flexible migration system for content updates, secure authentication across regions, and advanced filtering options. It focuses on improving workflow efficiency with features like organized file structures, customizable paths, and a dry run mode for migrations.
Common errors
-
command not found: storyblok
cause The Storyblok CLI package has not been installed globally or is not in your system's PATH.fixInstall the CLI globally using `npm install -g storyblok` or execute commands using `npx storyblok <command>`. -
Unauthorized: Please login first.
cause The CLI requires authentication to interact with your Storyblok space, and you have not logged in or your session has expired.fixRun `storyblok login` and provide your Storyblok Personal Access Token to authenticate your CLI session. -
Error: Minimum Node.js version 18.0.0 is required.
cause Your current Node.js version is older than the minimum requirement for the Storyblok CLI.fixUpdate Node.js to version 18.0.0 or higher. Use `nvm install 18 && nvm use 18` or download the latest LTS version from nodejs.org.
Warnings
- breaking Node.js 18.0.0 or higher is required. Older versions may lead to installation failures or runtime errors.
- gotcha A Storyblok Personal Access Token is mandatory for authentication. Without it, most CLI commands interacting with your space will fail.
- gotcha Be cautious when using commands like `storyblok push-components` or `storyblok migrate` on production spaces. Always use `--dry-run` first for migrations, and consider version control for component schemas.
- gotcha Path resolution for component and migration files can be tricky. Ensure the `--path` option is correctly specified relative to your current working directory or as an absolute path.
Install
-
npm install storyblok -
yarn add storyblok -
pnpm add storyblok
Imports
- storyblok
import { storyblok } from 'storyblok'storyblok <command> [options]
- npx storyblok
require('storyblok')().run()npx storyblok <command> [options]
- Generated Component Types
import { MyComponentType } from 'storyblok'import { MyComponentType } from './.storyblok/components'
Quickstart
npm install -g storyblok
storyblok login
# Follow the prompts to enter your personal access token
# Pull all component schemas from your space
storyblok pull-components --space=<YOUR_SPACE_ID> --path=./src/storyblok/components
# Generate TypeScript types for your components
storyblok generate-types --path=./src/storyblok/types.d.ts
# Run a simple migration (example: rename a field)
# First, create a migration file, e.g., 'storyblok/migrations/001-rename-field.js'
// storyblok/migrations/001-rename-field.js
module.exports = {
up: async (api) => {
console.log('Running migration: Rename old_field to new_field');
// Example: Find a component and rename a field
// await api.put('spaces/YOUR_SPACE_ID/components/COMPONENT_ID', { component: { schema: { new_field: { type: 'text' } } } });
},
down: async (api) => {},
};
storyblok migrate --space=<YOUR_SPACE_ID> --path=./storyblok/migrations --dry-run
storyblok migrate --space=<YOUR_SPACE_ID> --path=./storyblok/migrations