Skip to content

CLI Usage

grafana-react includes a CLI for building and validating dashboards.

Terminal window
npx grafana-react build my-dashboard.tsx

Build a single dashboard file to JSON:

Terminal window
npx grafana-react build <input.tsx> [output.json]

Arguments:

  • input.tsx - Path to the dashboard file
  • output.json - (Optional) Output path. If omitted, prints to stdout.

Examples:

Terminal window
# Output to stdout
npx grafana-react build my-dashboard.tsx
# Output to file
npx grafana-react build my-dashboard.tsx output/my-dashboard.json

Build all dashboard files in a directory:

Terminal window
npx grafana-react build-all <input-dir> [output-dir]

Finds all *.dashboard.tsx files and builds them.

Arguments:

  • input-dir - Directory containing dashboard files
  • output-dir - (Optional) Output directory. Defaults to same as input.

Examples:

Terminal window
# Build all dashboards, output alongside source files
npx grafana-react build-all ./dashboards
# Build to separate output directory
npx grafana-react build-all ./dashboards ./output

Validate a dashboard without generating output:

Terminal window
npx grafana-react validate <input.tsx>

Checks that the dashboard:

  • Parses correctly
  • Has valid component structure
  • Has required props

Examples:

Terminal window
npx grafana-react validate my-dashboard.tsx

Watch for changes and rebuild automatically:

Terminal window
npx grafana-react watch <input-dir> [output-dir]

Watches all *.dashboard.tsx files and rebuilds on change.

Arguments:

  • input-dir - Directory to watch
  • output-dir - (Optional) Output directory

Examples:

Terminal window
# Watch and build in place
npx grafana-react watch ./dashboards
# Watch with separate output
npx grafana-react watch ./dashboards ./output

Dashboard files should be named with the .dashboard.tsx suffix:

my-dashboard.tsx
system-overview.dashboard.tsx
api-monitoring.dashboard.tsx

The CLI uses this pattern to identify dashboard files.

The generated JSON is formatted and ready for import into Grafana:

{
"uid": "my-dashboard",
"title": "My Dashboard",
"tags": ["monitoring"],
"panels": [...],
"templating": {...},
...
}
CodeMeaning
0Success
1Build error (invalid JSX, missing props, etc.)
2File not found
{
"scripts": {
"build:dashboards": "grafana-react build-all ./dashboards ./output",
"dev:dashboards": "grafana-react watch ./dashboards ./output"
}
}
# GitHub Actions example
- name: Build dashboards
run: npx grafana-react build-all ./dashboards ./output
- name: Upload dashboards
uses: actions/upload-artifact@v4
with:
name: dashboards
path: output/*.json