Imports guide recommends a buggy use of import.meta.url
#7785
Labels
help wanted
Issues looking for someone to run with them!
improve documentation
Enhance existing documentation (e.g. add an example, improve description)
๐ Subject area/topic
Imports
๐ Page(s) affected (or suggested, for new content)
https://docs.astro.build/en/guides/imports/#node-builtins
๐ Description of content that is out-of-date or incorrect
The โNode Builtinsโ section in the Imports guide is three years old and probably misleading.
It suggests building a relative path to another file like this:
However,
import.meta.url
โs value varies between dev and build, for example during dev, a page atsrc/pages/dir/index.astro
has animport.meta.url
ofsrc/pages/dir/index.astro
. However, during a build this might be something likedist/chunks/prerender_CX4g21Zw.mjs
instead.In simple cases this technique can appear to work. For example, in
src/pages/index.astro
the folder depth is usually the same between dev and build and if youโre loading something at the root level likepackage.json
, you will end up in the right place.But in pages or components in subdirectories, or when trying to reference siblings in the file structure, youโre very likely to hit inconsistencies between dev and build (not to mention SSR uses of
node:fs
where you might even be referencing a file outside your project which is missing completely when deployed).๐ฅ๏ธ Reproduction in StackBlitz (if reporting incorrect content or code samples)
https://stackblitz.com/edit/github-9wjfwu?file=src%2Fpages%2Findex.astro
This example shows the documented technique apparently working in dev mode.
If you quit the dev server (Ctrl + C) and preview the build instead by running
npm run build && npm run preview
, the values in the sub directory page will be off.The text was updated successfully, but these errors were encountered: