remotion-dev/remotion · error · Error

Could not locate the JSX reorder target

Error message

Could not locate the JSX reorder target

What it means

getAdjacentJsxInsertionSourceEdit computes an insertion point just before or after a target JSX element to implement reordering. It throws when the target JSXElement has no source location, so the codemod cannot determine where to insert the sibling markup.

Solutions

  1. Pick the reorder target from the original recast-parsed tree so it carries loc.
  2. If the target was newly created, first insert it via an edit, re-parse, then reorder using the re-parsed node.
  3. Restore loc on cloned nodes or keep the original node reference instead of spreading/cloning.
  4. Guard with if (!target.loc) in your codemod and skip with a warning instead of throwing deep inside the library.

Example fix

// before
applySourceEdits({input, edits: [getAdjacentJsxInsertionSourceEdit({target: clonedEl, ...})]});
// after
const realEl = findElementInParsedSource(input, elId);
applySourceEdits({input, edits: [getAdjacentJsxInsertionSourceEdit({target: realEl, ...})]});
Defensive patterns

Strategy: type-guard

Validate before calling

if (!target.loc) {
  throw new Error('Reorder target must be a parsed node with location');
}

Type guard

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

Try / catch

try {
  const edit = getAdjacentJsxInsertionSourceEdit({input, insertion, position, target});
} catch (err) {
  // re-parse input and retry with the corresponding parsed node
}

Prevention

When it happens

Trigger: Calling with a target JSXElement synthesized in-memory (no loc), or an element whose loc was dropped by cloning/mutation in a prior transform step.

Common situations: Reordering elements in Studio's timeline codemods when the target was produced by a previous edit; parsing source without recast loc recording; moving a newly-created component into an existing sibling list.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at packages/studio-codemods/src/source-edits.ts:102

				? line.slice(originalIndent.length)
				: line.trimStart();
		})
		.join(getEndOfLine(input));
};

export const getAdjacentJsxInsertionSourceEdit = ({
	input,
	insertion,
	position,
	target,
}: {
	input: string;
	insertion: string;
	position: 'after' | 'before';
	target: JSXElement;
}): SourceEdit => {
	if (!target.loc) {
		throw new Error('Could not locate the JSX reorder target');
	}

	const targetStart = recastLocToOffset(input, target.loc.start);
	const targetEnd = recastLocToOffset(input, target.loc.end);
	const indent = getLineIndent({input, offset: targetStart});
	const endOfLine = getEndOfLine(input);

	if (position === 'after') {
		return {
			start: targetEnd,
			end: targetEnd,
			replacement: `${endOfLine}${indentInsertedJsx({indent, insertion})}`,
		};
	}

	const continuation = insertion
		.split(/\r?\n/)
		.map((line, index) => (index === 0 ? line : `${indent}${line}`))

View on GitHub (pinned to 46a3a6bf13)