Contentful Batch Utilities Library
This library provides core modules and shared methods utilized by Contentful's batch utility CLI tools, specifically for `contentful-import` and `contentful-export`. It offers low-level programmatic access to operations such as getting source content from a space, pushing changes (including deletions, creations, and publishing/unpublishing) to a destination space, and various content transformations. It also includes utilities for creating Contentful clients and error handling. The current stable version is 11.0.0. The project uses `semantic-release`, indicating a release cadence of frequent patch/minor versions and major versions only for breaking changes. Its key differentiator lies in providing granular, batch-oriented control over Contentful space manipulation, acting as a foundational layer for more complex data migration and synchronization workflows between Contentful environments.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module ... from ... not supported.
cause Attempting to use `require()` to import a module from `contentful-batch-libs` in a CommonJS context when the library is published as an ESM module.fixConvert your project or the specific file to use ESM imports: `import { createClients } from 'contentful-batch-libs';`. Ensure your `package.json` has `"type": "module"` if running in Node.js, or use a bundler that handles ESM correctly. -
Error: Node.js vX.Y.Z is not supported. Please upgrade to Node.js >=22.
cause Running `contentful-batch-libs` on a Node.js version older than the minimum required version (Node.js 22+ since v11.0.0).fixUpgrade your Node.js installation to version 22 or newer. Use a tool like `nvm` to manage Node.js versions: `nvm install 22 && nvm use 22`.
Warnings
- breaking Node.js versions older than 22 are no longer supported. The `engines.node` minimum has been raised to `>=22` starting with v11.0.0. Ensure your runtime environment meets this requirement before upgrading.
- breaking Version `v10.1.7` contained the Node.js 22 breaking change but was mistakenly released as a patch version. It should have been a major release. This was corrected in `v11.0.0`. Do not upgrade directly from `v10.1.6` to `v10.1.7` without verifying Node.js version.
- gotcha This library is primarily designed for ESM (ECMAScript Module) usage, especially from v11 onwards, aligning with modern Node.js practices. Attempting to `require()` it in a CommonJS context without proper transpilation or configuration can lead to import errors.
Install
-
npm install contentful-batch-libs -
yarn add contentful-batch-libs -
pnpm add contentful-batch-libs
Imports
- createClients
const { createClients } = require('contentful-batch-libs');import { createClients } from 'contentful-batch-libs'; - getSourceSpace
import { getSourceSpace } from 'contentful-batch-libs'; - pushToSpace
import { pushToSpace } from 'contentful-batch-libs';
Quickstart
import { createClients, getSourceSpace, pushToSpace } from 'contentful-batch-libs';
const SPACE_ID = process.env.CONTENTFUL_SOURCE_SPACE_ID ?? '';
const MANAGEMENT_TOKEN = process.env.CONTENTFUL_MANAGEMENT_TOKEN ?? '';
const DELIVERY_TOKEN = process.env.CONTENTFUL_DELIVERY_TOKEN ?? '';
const ENVIRONMENT_ID = process.env.CONTENTFUL_ENVIRONMENT_ID ?? 'master';
if (!SPACE_ID || !MANAGEMENT_TOKEN || !DELIVERY_TOKEN) {
console.error('Environment variables CONTENTFUL_SOURCE_SPACE_ID, CONTENTFUL_MANAGEMENT_TOKEN, and CONTENTFUL_DELIVERY_TOKEN must be set.');
process.exit(1);
}
async function runBatchOperation() {
try {
// 1. Create Contentful client instances
const { managementClient, deliveryClient } = createClients({
spaceId: SPACE_ID,
managementToken: MANAGEMENT_TOKEN,
accessToken: DELIVERY_TOKEN,
environmentId: ENVIRONMENT_ID,
});
console.log('Contentful clients created successfully.');
// 2. Get content from the source space
console.log(`Fetching content from space '${SPACE_ID}'...`);
const sourceContent = await getSourceSpace({
managementClient,
deliveryClient,
spaceId: SPACE_ID,
environmentId: ENVIRONMENT_ID,
// You might want to specify content types, entries, assets, etc. to fetch
// For example: query: { 'content_type': 'blogPost' }
});
console.log(`Fetched ${sourceContent.entries.length} entries and ${sourceContent.assets.length} assets.`);
// 3. (Optional) Transform content here if needed
// const transformedContent = transformSpace(sourceContent, /* transformers */);
// 4. Push transformed or original content to a destination space (example uses the same space ID for simplicity)
// In a real scenario, you'd likely have a separate destination space ID and clients.
console.log(`Pushing content back to space '${SPACE_ID}'...`);
await pushToSpace({
sourceContent,
managementClient,
spaceId: SPACE_ID,
environmentId: ENVIRONMENT_ID,
// Optional: publish: false, contentModelOnly: true, etc.
});
console.log('Batch operation completed successfully.');
} catch (error) {
console.error('An error occurred during the batch operation:', error);
process.exit(1);
}
}
runBatchOperation();