remotion-dev/remotion · error

Could not locate the folder insertion target

Error message

Could not locate the folder insertion target

What it means

Thrown inside getNewFolderSourceEdits' visitNode callback (folder-source-edits.ts:196). After a `new-folder` codemod has grafted a new, location-less `<Folder>` node into the AST, the codemod needs an anchor node that carries source location (`loc`) to compute the text insertion edit. If neither the visited JSX node nor any of its located children provides a usable JSXElement/JSXFragment root, insertion is impossible and this error is thrown.

Solutions

  1. Reformat/save the Root file so the JSX tree is written normally with stable source positions and retry the folder creation
  2. Add the new <Folder> wrapper manually in the editor around the target compositions
  3. Ensure the compositions live inside a directly-written JSXElement/JSXFragment rather than a spread or variable

Example fix

// before (unanchored, programmatically assembled tree)
const children = [<Composition id="A" />];
export const RemotionRoot = () => <Root>{children}</Root>;

// after (statically written, locatable JSX)
export const RemotionRoot = () => (
  <Root>
    <Folder name="My Folder">
      <Composition id="A" />
    </Folder>
  </Root>
);
Defensive patterns

Strategy: validation

Validate before calling

if (!/<(Root|Composition|Folder)[\s>]/.test(rootFileSource)) {
  throw new Error('Root file has no locatable JSX elements; add folders manually');
}

Type guard

const hasSourceLocation = (node: {loc: unknown} | null): node is {loc: nonNullable} =>
  node !== null && node.loc !== null && node.loc !== undefined;

Try / catch

try {
  editFolderInSource({codeMod, input});
} catch (e) {
  if (e instanceof Error && e.message === 'Could not locate the folder insertion target') {
    // prompt user to create the <Folder> manually
  }
}

Prevention

When it happens

Trigger: A `new-folder` codemod runs on a file where applyCodemod grafted the new Folder into a JSX tree whose nodes have no recast source locations — typically because the enclosing JSX node itself lacks `loc` and no located child exists to anchor against.

Common situations: Root files with unusual formatting that confuses recast's location tracking, files where the composition tree is assembled from fragments/variables without positions, or heavily machine-generated code where the new node was inserted into an unlocatable subtree.

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/fcb8f7970b2f9552. Report an issue: GitHub.

Appendix: source

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

				this.traverse(path);
				return undefined;
			}

			inserted.openingElement.name = recast.types.builders.jsxIdentifier(
				localName,
			) as never;
			const root = jsxNode.loc
				? jsxNode
				: jsxNode.children.find(
						(child) =>
							(child.type === 'JSXElement' || child.type === 'JSXFragment') &&
							child.loc,
					);
			if (
				!root ||
				(root.type !== 'JSXElement' && root.type !== 'JSXFragment')
			) {
				throw new Error('Could not locate the folder insertion target');
			}

			insertionEdit = getInsertionRootSourceEdit({
				input,
				insertion: printInsertedJsx({
					element: inserted as never,
					input,
					prettierConfigOverride: null,
				}),
				root: root as never,
				nullRoot: null,
				prettierConfigOverride: null,
				insertInside: Boolean(jsxNode.loc),
			});
			return false;
		},
	});

View on GitHub (pinned to 46a3a6bf13)