Registry /
type-stubs / contentful-typescript-codegen
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
CLI Tool
✓ npx contentful-typescript-codegen --output path/to/types.d.ts
✗ import contentfulCodegen from 'contentful-typescript-codegen'
This package is a command-line utility; it is not typically imported into application code. Its primary usage is via `npx` or a package.json script.
EnvironmentGetter
✓ import { EnvironmentGetter } from 'contentful-typescript-codegen'
This type is provided by the package to help strongly type the function exported by your `getContentfulEnvironment.ts` file.
Generated Types (Example)
✓ import { IBlogPost, IPage, TypeBlogPostFields } from '@types/generated/contentful'
The generated types (e.g., IBlogPost, IPage) are imported from the output file specified in your CLI command (e.g., `contentful.d.ts`).
This quickstart demonstrates setting up `contentful-typescript-codegen` in a project, including the necessary `package.json` script, the `getContentfulEnvironment.ts` configuration file for fetching your Contentful schema, and how to execute the code generation. It highlights the use of `EnvironmentGetter` and the importance of environment variables.
{
"name": "my-contentful-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"contentful-codegen": "contentful-typescript-codegen --output @types/generated/contentful.d.ts"
},
"devDependencies": {
"contentful-typescript-codegen": "^3.4.0",
"contentful-management": "^10.0.0",
"ts-node": "^10.0.0",
"typescript": "^5.0.0",
"prettier": "^2.0.0"
}
}
// File: getContentfulEnvironment.ts
import { strict as assert } from "assert";
import contentfulManagement from "contentful-management";
import { EnvironmentGetter } from "contentful-typescript-codegen";
const CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN = process.env.CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN ?? '';
const CONTENTFUL_SPACE_ID = process.env.CONTENTFUL_SPACE_ID ?? '';
const CONTENTFUL_ENVIRONMENT = process.env.CONTENTFUL_ENVIRONMENT ?? '';
assert(CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN, "CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN is required.");
assert(CONTENTFUL_SPACE_ID, "CONTENTFUL_SPACE_ID is required.");
assert(CONTENTFUL_ENVIRONMENT, "CONTENTFUL_ENVIRONMENT is required.");
const getContentfulEnvironment: EnvironmentGetter = () => {
const contentfulClient = contentfulManagement.createClient({
accessToken: CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN,
});
return contentfulClient
.getSpace(CONTENTFUL_SPACE_ID)
.then((space) => space.getEnvironment(CONTENTFUL_ENVIRONMENT));
};
export = getContentfulEnvironment;
// To run:
// 1. Set environment variables: CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN, CONTENTFUL_SPACE_ID, CONTENTFUL_ENVIRONMENT
// 2. yarn install
// 3. yarn contentful-codegen
contentful-typescript-codegen --version
Errors
Common errors & fixes
Error: Cannot find module 'ts-node'
Attempting to use a `getContentfulEnvironment.ts` file without `ts-node` installed.
fixInstall `ts-node` as a development dependency: `yarn add --dev ts-node` or `npm install --save-dev ts-node`.
Error: Cannot find module 'contentful-management'
The `getContentfulEnvironment.js` or `getContentfulEnvironment.ts` file tries to import `contentful-management` but it's not installed.
fixInstall `contentful-management` as a development dependency: `yarn add --dev contentful-management` or `npm install --save-dev contentful-management`.
AssertionError: CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN is required.
Required environment variables (like access token, space ID, or environment name) are not set when the codegen tool runs.
fixEnsure `CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN`, `CONTENTFUL_SPACE_ID`, and `CONTENTFUL_ENVIRONMENT` are set in your shell or `.env` file before running the codegen script.
ENOENT: no such file or directory, open 'path/to/types.d.ts'
The specified output directory for the generated types does not exist.
fixCreate the target directory before running the codegen command (e.g., `mkdir -p @types/generated`).
Audit
Dependencies
ts-noderequiredRequired as a peer dependency if the environment configuration file (getContentfulEnvironment.ts) is written in TypeScript.
prettieroptionalRequired as a peer dependency for formatting the generated TypeScript files according to the project's Prettier configuration.
contentful-managementrequiredImplicitly required by the user's getContentfulEnvironment.js/ts file to connect to the Contentful Management API and fetch content models.