Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import.meta.env. get error Unexpected "export" in astro

I am facing a problem with using the environment variable in an Astro project.

My .env file has PUBLIC_DOMAIN = 'https://abc.com.vn/'.

The structure of my project is:

The problem is, when I type const DOMAIN = import.meta.env.PUBLIC_DOMAIN in any .astro file in the components, it works fine and I can get PUBLIC_DOMAIN. But if a file uses that line in any file in pages/[lang], it causes an error like Unexpected "export".

The error caused by the code looks like:

import {getLangFromUrl, useTranslations} from '../../i18n/utils'
import {languages} from '../../i18n/ui'
const DOMAIN = import.meta.env.PUBLIC_DOMAIN

const lang = getLangFromUrl(Astro.url)
const t = useTranslations(lang)

export async function getStaticPaths() {
    return languages.map((lang) => {
        return {params: {lang}}
    })
}

I want to ask why this is happening, and how I can solve it. Thanks so much for any support.

like image 400
Nghĩa Phạm Avatar asked Oct 23 '25 15:10

Nghĩa Phạm


1 Answers

I have finally found out that I have to delete the ‘.git’ line from the .gitignore file :))

like image 123
Nghĩa Phạm Avatar answered Oct 27 '25 06:10

Nghĩa Phạm