sveltejs/kit · error · Error

Could not locate ${file_path}. Please move it to be located

Error message

Could not locate ${file_path}. Please move it to be located relative to the page in the routes directory or reference it beginning with /static/. See https://vitejs.dev/guide/assets for more details on referencing assets.

What it means

When an <img> src cannot be resolved by Vite but a file with that path exists in the static/public directory, enhanced-img throws this longer message: it cannot optimize public-dir assets, so the asset must instead live next to the route or be referenced via a resolvable import.

Source

Thrown at packages/enhanced-img/src/vite-plugin.js:139

						const width = get_attr_value(node, 'width');
						url += url.includes('?') ? '&' : '?';
						if (sizes && 'raw' in sizes) {
							url += 'imgSizes=' + encodeURIComponent(sizes.raw) + '&';
						}
						if (width && 'raw' in width) {
							url += 'imgWidth=' + encodeURIComponent(width.raw) + '&';
						}
						url += 'enhanced';
					}

					// resolves the import so that we can build the entire picture template string and don't
					// need any logic blocks
					const resolved_id = (await plugin_context.resolve(url, filename))?.id;
					if (!resolved_id) {
						const query_index = url.indexOf('?');
						const file_path = query_index >= 0 ? url.substring(0, query_index) : url;
						if (existsSync(path.resolve(vite_config.publicDir, file_path))) {
							throw new Error(
								`Could not locate ${file_path}. Please move it to be located relative to the page in the routes directory or reference it beginning with /static/. See https://vitejs.dev/guide/assets for more details on referencing assets.`
							);
						}
						throw new Error(
							`Could not locate ${file_path}. See https://vitejs.dev/guide/assets for more details on referencing assets.`
						);
					}

					if (OPTIMIZABLE.test(url)) {
						const image = await process_id(resolved_id, plugin_context, imagetools_plugin);
						s.update(node.start, node.end, img_to_picture(content, node, image));
					} else {
						const metadata = await sharp(resolved_id).metadata();
						// this must come after the await so that we don't hand off processing between getting
						// the imports.size and incrementing the imports.size
						const name = imports.get(original_url) || '__IMPORTED_ASSET_' + imports.size + '__';
						if (!metadata.width || !metadata.height) {
							console.warn(`Could not determine intrinsic dimensions for ${resolved_id}`);

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Move the image out of static/ into the routes directory next to the page and reference it relatively
  2. Import the image in the component and bind the imported URL to src
  3. If the asset truly belongs in static, remove it from enhanced-img optimization expectations and reference it with a leading /static/ path as the message suggests

Example fix

<!-- before -->
<img src="/static/hero.png" alt="hero" />
<!-- after -->
<script>
import hero from './hero.png';
</script>
<img src={hero} alt="hero" />
Defensive patterns

Strategy: validation

Validate before calling

// before building, ensure referenced images resolve outside static/
const src = '/static/hero.png';
if (src.startsWith('/static/') || existsSync(path.join('static', src))) {
  console.warn('enhanced-img cannot optimize static-dir asset:', src);
}

Prevention

When it happens

Trigger: `update_element` (from RegularElement handling) calls `plugin_context.resolve(url)` which returns undefined, and `existsSync(publicDir + file_path)` is true — i.e. the image was found in the static directory.

Common situations: Referencing images as `<img src="/static/foo.png">` or `<img src="foo.png">` where foo.png only exists in the static folder instead of importing it or placing it relative to the route.

Related errors


AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02). Data as JSON: /api/errors/cbcace179ae70f3c. Report an issue: GitHub.