remotion-dev/remotion · error

Could not locate the composition to duplicate

Error message

Could not locate the composition to duplicate

What it means

duplicateCompositionInSource builds a duplicate of the matched <Composition> as an AST node, then needs the original element's source location to compute where to insert the copy. If original.loc is undefined (the matched node is not backed by real source text), the insertion offset cannot be derived and it throws.

Solutions

  1. Re-run duplicateCompositionInSource on the freshly parsed original file
  2. Save and reformat the file so the <Composition> exists as literal source, then retry
  3. Copy the <Composition> block manually in the editor

Example fix

null
Defensive patterns

Strategy: try-catch

Validate before calling

const hasLiteralComposition = new RegExp(`<Composition[^>]*id="${id}"`).test(input);
if (!hasLiteralComposition) throw new Error('Composition must exist as literal source to duplicate');

Type guard

const hasLoc = (n: {loc?: unknown}): n is {loc: NonNullable<unknown>} => n.loc != null;

Try / catch

try {
  duplicateCompositionInSource({input, codemod});
} catch (e) {
  if ((e as Error).message.includes('Could not locate the composition to duplicate')) {
    // reparse original file / save + format to disk, then retry
  }
}

Prevention

When it happens

Trigger: Visiting the JSX element whose id matches codemod.idToDuplicate where the element node has no loc — e.g. the composition was generated into the AST by a prior transformation rather than parsed from source.

Common situations: Duplicating a composition in a file that was previously machine-modified; chained codemods where an earlier step synthesized the Composition element.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of remotion-dev/remotion@46a3a6bf13 (2026-09-18). Data as JSON: /api/errors/a951a0951b5aaf4d. Report an issue: GitHub.

Appendix: source

Thrown at packages/studio-codemods/src/duplicate-composition.ts:239

	const changesMade: {description: string}[] = [];
	const edits: SourceEdit[] = [];
	const snapshots = captureImportSnapshots(ast);

	recast.types.visit(ast, {
		visitJSXElement(astPath) {
			const original = astPath.node as unknown as JSXElement;
			if (getCompositionId(original) !== codemod.idToDuplicate) {
				this.traverse(astPath);
				return undefined;
			}

			const duplicate = changeComposition({
				jsxElement: original,
				codemod,
				changesMade,
			});
			if (!original.loc) {
				throw new Error('Could not locate the composition to duplicate');
			}

			const insertion = printInsertedJsx({
				element: duplicate as never,
				input,
				prettierConfigOverride: null,
			});
			const parent = astPath.parentPath?.node;
			if (
				parent &&
				(parent.type === 'JSXElement' || parent.type === 'JSXFragment')
			) {
				const end = recastLocToOffset(input, original.loc.end);
				const start = recastLocToOffset(input, original.loc.start);
				const lineStart = input.lastIndexOf('\n', start - 1) + 1;
				const indent =
					input.slice(lineStart, start).match(/^[\t ]*/)?.[0] ?? '';
				const endOfLine = input.includes('\r\n') ? '\r\n' : '\n';

View on GitHub (pinned to 46a3a6bf13)