CLI Usage
grafana-react includes a CLI for building and validating dashboards.
npx grafana-react build my-dashboard.tsxCommands
Section titled “Commands”Build a single dashboard file to JSON:
npx grafana-react build <input.tsx> [output.json]Arguments:
input.tsx- Path to the dashboard fileoutput.json- (Optional) Output path. If omitted, prints to stdout.
Examples:
# Output to stdoutnpx grafana-react build my-dashboard.tsx
# Output to filenpx grafana-react build my-dashboard.tsx output/my-dashboard.jsonbuild-all
Section titled “build-all”Build all dashboard files in a directory:
npx grafana-react build-all <input-dir> [output-dir]Finds all *.dashboard.tsx files and builds them.
Arguments:
input-dir- Directory containing dashboard filesoutput-dir- (Optional) Output directory. Defaults to same as input.
Examples:
# Build all dashboards, output alongside source filesnpx grafana-react build-all ./dashboards
# Build to separate output directorynpx grafana-react build-all ./dashboards ./outputvalidate
Section titled “validate”Validate a dashboard without generating output:
npx grafana-react validate <input.tsx>Checks that the dashboard:
- Parses correctly
- Has valid component structure
- Has required props
Examples:
npx grafana-react validate my-dashboard.tsxWatch for changes and rebuild automatically:
npx grafana-react watch <input-dir> [output-dir]Watches all *.dashboard.tsx files and rebuilds on change.
Arguments:
input-dir- Directory to watchoutput-dir- (Optional) Output directory
Examples:
# Watch and build in placenpx grafana-react watch ./dashboards
# Watch with separate outputnpx grafana-react watch ./dashboards ./outputFile Naming Convention
Section titled “File Naming Convention”Dashboard files should be named with the .dashboard.tsx suffix:
my-dashboard.tsxsystem-overview.dashboard.tsxapi-monitoring.dashboard.tsxThe CLI uses this pattern to identify dashboard files.
Output Format
Section titled “Output Format”The generated JSON is formatted and ready for import into Grafana:
{ "uid": "my-dashboard", "title": "My Dashboard", "tags": ["monitoring"], "panels": [...], "templating": {...}, ...}Exit Codes
Section titled “Exit Codes”| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | Build error (invalid JSX, missing props, etc.) |
| 2 | File not found |
Integration with Build Systems
Section titled “Integration with Build Systems”package.json scripts
Section titled “package.json scripts”{ "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