#Error: Cannot access 'ASTRO_VERSION' before initialization

1 messages · Page 1 of 1 (latest)

jolly fox
#

STATUS: solved underlying problem by just using static redirects.

CONTEXT: cloudflare hosted starlight site at https://ratatui.rs
I'm trying to rename some content paths and add redirects.

Added cloudflare adapter, switched to hybrid mode added some ssr settings to the vite config (relevant commit)

Running npm astro build sees the following error:

Cannot access 'ASTRO_VERSION' before initialization
  Stack trace:
    at createAstro (file:///Users/joshka/local/ratatui-website/dist/_worker.js/chunks/astro_AtHjLoge.mjs:393:26)
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async BuildPipeline.retrieveManifest (file:///Users/joshka/local/ratatui-website/node_modules/astro/dist/core/build/pipeline.js:93:26)
    at async staticBuild (file:///Users/joshka/local/ratatui-website/node_modules/astro/dist/core/build/static-build.js:108:7)
    at async AstroBuilder.run (file:///Users/joshka/local/ratatui-website/node_modules/astro/dist/core/build/index.js:164:7)

I'm guessing that this is something to do with the way that the async happens here.
I'm not entirely sure how this file is generated (guessing it's vite, but I'm not a frontend dev)
Any ideas on how to fix this?

wintry magnet
#

Did you do npm astro build or npm run build?

If the former, try the latter

jolly fox
#

Same problem - npm astro build runs astro check && astro build, and the problem is in the astro build step.

wintry magnet
#

Okie, lemme look through that commit a bit

Also love your crate name houston_love

jolly fox
#

🙂

jolly fox
wintry magnet
#

Hmm tough to say, your implementation seems to be fine

jolly fox
#

I wonder if it's something to do with the ssr external section?

ssr: {
      external: ["node:url", "node:path", "node:child_process"],
    },

Added those to get around errors being spat out from the build steps, but I suspect they will fail on cloudflare as not supported APIs. https://developers.cloudflare.com/workers/runtime-apis/nodejs

wintry magnet
#

If you remove them are you still able to build locally?

jolly fox
#

no

wintry magnet
#

Hmmm

jolly fox
#
20:12:52 [ERROR] [vite] x Build failed in 261ms
[commonjs--resolver] [plugin vite:resolve] Cannot bundle Node.js built-in "node:url" imported from "node_modules/@astrojs/starlight/utils/route-data.ts". Consider disabling ssr.noExternal or remove the built-in dependency.
  Stack trace:
    at getRollupError (file:///Users/joshka/local/ratatui-website/node_modules/rollup/dist/es/shared/parseAst.js:394:41)
    at Object.error (file:///Users/joshka/local/ratatui-website/node_modules/rollup/dist/es/shared/node-entry.js:19593:20)
    at Object.handler (file:///Users/joshka/local/ratatui-website/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:67676:19)
    at async PluginDriver.hookFirstAndGetPlugin (file:///Users/joshka/local/ratatui-website/node_modules/rollup/dist/es/shared/node-entry.js:19678:28)
    at async ModuleLoader.resolveId (file:///Users/joshka/local/ratatui-website/node_modules/rollup/dist/es/shared/node-entry.js:18762:15)
#

(which might mean that using the cloudflare adapter isn't supported for starlight)

wintry magnet
#

Might not hurt to ask in #starlight to verify

jolly fox
#

done. I might try a minimal repro with starlight a bit later.

#

confirmed this is the same error seen for a standard starlight app, adding the cloudflare adapter

wintry magnet
#

Ah interesting, is there an issue for it on the repo? If not feel free to submit it houston_fingerguns

jolly fox
#

I think I'll just slap a bunch of static routes in there instead of the dynamic ones and call it a day

jade bridge
#

I haven’t tried with the Cloudflare adapter personally but I believe @mint lotus and @wary tendon have?

wary tendon
#

Yeah I tested it and it worked for me. But this 'ASTRO_VERSION' error comes up from time to time, not sure why, still trying to figure it out 🤔

jade bridge
#

Interesting. ASTRO_VERSION is not something Starlight accesses, so I guess it’s an issue on the Astro level?

jolly fox
#
node --version
v20.12.2

npm create astro@latest -- --template starlight
cd planetary-parsec
git commit -m 'init'
npm install
npm run build
(succeeds)
npx astro add cloudflare
npm run build
(fails)
03:15:05 [ERROR] [vite] x Build failed in 202ms
[commonjs--resolver] [plugin vite:resolve] Cannot bundle Node.js built-in "node:url" imported from "node_modules/@astrojs/starlight/utils/route-data.ts". Consider disabling ssr.noExternal or remove the built-in dependency.
  Stack trace:
    at getRollupError (file:///Users/joshka/local/planetary-parsec/node_modules/rollup/dist/es/shared/parseAst.js:394:41)
    at Object.error (file:///Users/joshka/local/planetary-parsec/node_modules/rollup/dist/es/shared/node-entry.js:19593:20)
    at Object.handler (file:///Users/joshka/local/planetary-parsec/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:67676:19)
    at async PluginDriver.hookFirstAndGetPlugin (file:///Users/joshka/local/planetary-parsec/node_modules/rollup/dist/es/shared/node-entry.js:19678:28)
    at async ModuleLoader.resolveId (file:///Users/joshka/local/planetary-parsec/node_modules/rollup/dist/es/shared/node-entry.js:18762:15)
#

added the ssr settings that get past that, boom:

Cannot access 'ASTRO_VERSION' before initialization
  Stack trace:
    at createAstro (file:///Users/joshka/local/planetary-parsec/dist/_worker.js/chunks/astro_BiHw7Ve8.mjs:393:26)
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
    at async BuildPipeline.retrieveManifest (file:///Users/joshka/local/planetary-parsec/node_modules/astro/dist/core/build/pipeline.js:93:26)
    at async staticBuild (file:///Users/joshka/local/planetary-parsec/node_modules/astro/dist/core/build/static-build.js:108:7)
    at async AstroBuilder.run (file:///Users/joshka/local/planetary-parsec/node_modules/astro/dist/core/build/index.js:164:7)
jade bridge
#

Thanks for the details. Way beyond my knowledge of SSR to be of much use. My understanding was we’d built SSR support in Starlight in such a way that things like that @astrojs/starlight/utils/route-data would run at build time and not need to run in Cloudflare’s environment.

#

Although it’s also odd that as Alex mentioned, he has a Starlight site deployed to Cloudflare SSR, so I guess this problem only occurs in specific circumstances.

jolly fox
#

To be clear, the only reason I was worried about this was that I tried to get dynamic redirects to work, which turned into a yak shave. The underlying goal is achieved by using static redirects for the pages in question instead of dynamic.

obtuse bridge
#

You can also do it by adding it in wrangler.toml

jolly fox
mint lotus
#

Are you running the build portion inside of Cloudflare? That is when that stack trace happens.
I'd suggest building in your preferred CI and deploy just the build result to Cloudflare

jolly fox
#

This is a local build.

#

Minimal repro steps are noted in #1242301878994468916 message with a link to a minimal repro repo just after that

mint lotus
#

Interesting

#

I'll take a look later

jolly fox
#

(no rush - the underlying goal is satisfied)

plain vale
#

I just stumbled across this on a non-startlight site after updating astro - just wondering if anyone had a further look