Installation
Esta página aún no está disponible en tu idioma.
System Requirements
Section titled “System Requirements”- Node.js: Version 18.0 or higher
- Strapi: Version 4.x or 5.x
- Package Manager: npm, pnpm, yarn, or bun
Installation
Section titled “Installation”npm install -D strapi2frontpnpm add -D strapi2frontyarn add -D strapi2frontbun add -D strapi2frontCreating a Configuration File
Section titled “Creating a Configuration File”Run the init command to create a configuration file:
npx strapi2front initThis will create a strapi.config.ts file with sensible defaults.
Strapi API Tokens
Section titled “Strapi API Tokens”strapi2front uses two separate tokens for different purposes:
1. Sync Token (STRAPI_SYNC_TOKEN)
Section titled “1. Sync Token (STRAPI_SYNC_TOKEN)”Used by the CLI to fetch your schema during development. Create one in the Strapi admin panel:
- Go to Settings > API Tokens
- Click Create new API Token
- Give it a name like “strapi2front sync”
- Set the token type to Custom
- Configure the following permissions:
Required permissions:
| Plugin | Permission | Description |
|---|---|---|
| Content-type-builder | getComponents | Read component schemas |
| Content-type-builder | getComponent | Read individual component |
| Content-type-builder | getContentTypes | Read content type schemas |
| Content-type-builder | getContentType | Read individual content type |
Optional permissions (if using i18n):
| Plugin | Permission | Description |
|---|---|---|
| I18n | listLocales | Read available locales |
2. Frontend Token (STRAPI_TOKEN)
Section titled “2. Frontend Token (STRAPI_TOKEN)”Used by your generated client to fetch content at runtime. Create a separate token with only the permissions your app needs:
- Go to Settings > API Tokens
- Click Create new API Token
- Give it a name like “Frontend”
- Set the token type to Custom or Read-only (depending on your needs)
- Grant only the permissions your frontend needs (e.g.,
findandfindOnefor your content types)
Environment Variables
Section titled “Environment Variables”Create a .env file in your project root:
STRAPI_URL=http://localhost:1337
# Sync token: Used by strapi2front to sync schema (development only)# Permissions: content-type-builder, i18nSTRAPI_SYNC_TOKEN=your-sync-token-here
# Frontend token: Used by your app to fetch content (production)# Configure with only the permissions your app needsSTRAPI_TOKEN=your-frontend-token-hereThen reference them in your config:
import { defineConfig } from "strapi2front";
export default defineConfig({ url: process.env.STRAPI_URL || "http://localhost:1337", // Uses STRAPI_SYNC_TOKEN for sync, falls back to STRAPI_TOKEN token: process.env.STRAPI_SYNC_TOKEN || process.env.STRAPI_TOKEN, // ... rest of config});TypeScript vs JavaScript
Section titled “TypeScript vs JavaScript”strapi2front supports both TypeScript and JavaScript projects:
TypeScript (default)
Section titled “TypeScript (default)”import { defineConfig } from "strapi2front";
export default defineConfig({ outputFormat: "typescript", // .ts files with type annotations // ...});JavaScript with JSDoc
Section titled “JavaScript with JSDoc”import { defineConfig } from "strapi2front";
export default defineConfig({ outputFormat: "jsdoc", // .js files with JSDoc comments // ...});Verifying Installation
Section titled “Verifying Installation”Run the sync command to test your setup:
npx strapi2front syncIf successful, you’ll see output similar to:
◇ strapi2front sync│◇ Configuration loaded│◇ Strapi v5│◇ Schema fetched: 3 collections, 1 singles, 5 components│◇ Generated 18 files│┌─────────────────────────────────────────────────────────────┐│ ││ Generated 18 files in src/strapi ││ ││ Files generated: ││ src/strapi/shared/utils.ts ││ src/strapi/shared/client.ts ││ src/strapi/shared/locales.ts ││ src/strapi/collections/article/types.ts ││ src/strapi/collections/article/schemas.ts ││ src/strapi/collections/article/service.ts ││ src/strapi/collections/article/actions.ts ││ ... and 11 more ││ │└─────────────────────────────────────────────────────────────┘│└ Types, Services, Schemas, Actions ready to use!Troubleshooting
Section titled “Troubleshooting”Connection refused
Section titled “Connection refused”Make sure your Strapi server is running and the URL is correct.
401 Unauthorized
Section titled “401 Unauthorized”Your API token may be invalid or expired. Generate a new one in Strapi admin.
Could not find strapi.config
Section titled “Could not find strapi.config”Run npx strapi2front init to create the configuration file.