I have an astro project in which I’m building a custom typescript script for creating draft blog posts. I am running the script directly using tsx.
When I run the script, I always get an error when I try to import from "astro:content"
.
utils.ts
script file to be run with tsx
. It worked until I added an import from "astro:content"
:
import { getEntry, type CollectionEntry } from "astro:content";
...
tsx v4.11.0
is installed globally
node v21.6.0
is used
command run, using the default astro tsconfig
:
tsx --tsconfig ./tsconfig.json ./src/utils/utils.ts create-post
tsconfig.json
:
{
"extends": "astro/tsconfigs/base",
"compilerOptions": {
"strictNullChecks": true,
"allowJs": true,
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
output:
Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. Received protocol 'astro:'
I want to reference a content collection (of photos) with this script for generating a draft blog post in another content collection. I want to do this programatically, outside of the astro runtime/build.
The script worked, but once I added the import for getEntry
from astro:content
I started getting the above error. The same import works inside .astro components and layouts, but not as mentioned above in a standalone script. I am not sure if the issue is with tsx
, tsconfig.json
, or something astro specific.