#Cannot find module 'astro:content'

3 messages · Page 1 of 1 (latest)

latent karma
#

I need to import from content.config.ts
import { Features, ValidItems } from "../content.config";
in one of my .ts files. This error raises
Debugger attached.
[astro] Unable to load your Astro config

Cannot find module 'astro:content' imported from 'E:/www/Juni_Astro/src/content.config.ts'
Stack trace:
at fetchModule (file:///E:/www/Juni_Astro/node_modules/vite/dist/node/chunks/config.js:33984:34)
at fetchModule (file:///E:/www/Juni_Astro/node_modules/vite/dist/node/chunks/config.js:34794:17)
at EventEmitter.listenerForInvokeHandler (file:///E:/www/Juni_Astro/node_modules/vite/dist/node/chunks/config.js:25892:19)
at Object.send (file:///E:/www/Juni_Astro/node_modules/vite/dist/node/chunks/config.js:15052:39)
at Object.invoke (file:///E:/www/Juni_Astro/node_modules/vite/dist/node/module-runner.js:584:31)
Waiting for the debugger to disconnect...
Removing import { Features, ValidItems } from "../content.config"; fixes error.
vars used in content.config.ts are
export const Features = [
...strings
] as const;
export const ValidItems = [
...strings
] as const;

scarlet yachtBOT
#

Hi @latent karma. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The error occurs because your .ts file is importing directly from content.config.ts, which itself imports from astro:content. The astro:content module is a virtual module that only exists within Astro's build/runtime context — it cannot be resolved when your file is loaded outside of that context (e.g., during config loading). [Content Collections API]

The Fix

Move your shared constants (Features, ValidItems) out of content.config.ts into a separate plain .ts file that does not import from astro:content. Then import from that file in both your content.config.ts and your other .ts files.

Step 1: Create a new file, e.g. src/lib/constants.ts:

export const Features = [
  // ...strings
] as const;

export const ValidItems = [
  // ...strings
] as const;

Step 2: Import from that file in content.config.ts:

import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';
import { Features, ValidItems } from './lib/constants';

// use Features/ValidItems in your schema...
export const collections = { ... };

Step 3: Import from that same file in your other .ts file:

import { Features, ValidItems } from '../lib/constants';

This way, your shared constants live in a plain TypeScript file with no dependency on astro:content, so they can be safely imported anywhere without triggering the module resolution error.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: