#PUBLIC_* Environmental variables not being read

9 messages · Page 1 of 1 (latest)

lone tulip
#

I believe I have followed the docs to the letter.

I have this in my .env file:

PUBLIC_EMAILJS_USER_ID=Blah1
PUBLIC_EMAILJS_SERVICE_ID=Blah2
PUBLIC_EMAILJS_TEMPLATE_ID=Blah3

I use this code to get the values:

const emailjsUserId = import.meta.env.PUBLIC_EMAILJS_USER_ID; const emailjsServiceId = import.meta.env.PUBLIC_EMAILJS_SERVICE_ID; const emailjsTemplateId = import.meta.env.PUBLIC_EMAILJS_TEMPLATE_ID;

However, the variables are always empty. Thus, this code throws the exception:

if (!emailjsUserId || !emailjsServiceId || !emailjsTemplateId) { throw new Error('EmailJS environment variables are not set in code.'); }
I'm clearly missing something obvious.

chilly arrowBOT
#

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

#

Based on the information provided in the extracts, there could be a few reasons why your environment variables are not being loaded correctly.

  1. Astro Configuration Files: According to the Astro documentation (https://docs.astro.build/en/guides/configuring-astro/), Astro evaluates configuration files before it loads your other files. As such, you can’t use import.meta.env to access environment variables that were set in .env files in a configuration file. If you're trying to access these variables in a configuration file, this could be the issue.

  2. Prefixing: The Astro documentation (https://docs.astro.build/en/guides/environment-variables/) also mentions that while all environment variables are available in server-side code, only environment variables prefixed with PUBLIC_ are available in client-side code for security purposes. You've done this correctly, but it's worth double-checking that your code is running on the client-side.

  3. File Location: Ensure that your .env file is located in the root of your project. Astro will check the server environment for variables, if they don’t exist, Astro will look for them in .env files.

  4. Runtime Variables: If you're using Astro's Node runtime, the Astro documentation (https://docs.astro.build/en/guides/integrations-guide/node/) mentions that environment variables present during the build process will be hard-coded in the output. If these variables are meant to be runtime variables, they should be absent from the .env file during the build, and provided to Astro at runtime.

#

@lone tulip kapa.ai is still learning and improving, please let us know how it did by reacting below

lone tulip
#

@chilly arrow If I can't use import.meta.env, what should I use?

chilly arrowBOT
#

Let me check the knowledge base for you. One moment! :books:

#

If you can't use import.meta.env to access environment variables, you have a few alternatives:

  1. process.env: In some contexts, such as configuration files or when using SSR, you can use process.env to access environment variables. This is mentioned in the Astro documentation (https://docs.astro.build/en/guides/configuring-astro/).
const emailjsUserId = process.env.PUBLIC_EMAILJS_USER_ID;
const emailjsServiceId = process.env.PUBLIC_EMAILJS_SERVICE_ID;
const emailjsTemplateId = process.env.PUBLIC_EMAILJS_TEMPLATE_ID;
  1. Vite’s loadEnv helper: You can also use Vite’s loadEnv helper to manually load .env files. This is particularly useful in configuration files where import.meta.env is not available. Note that if you're using pnpm, you'll need to install vite to use the loadEnv helper. This is also mentioned in the Astro documentation (https://docs.astro.build/en/guides/configuring-astro/).
import { loadEnv } from "vite";

const env = loadEnv(process.env.NODE_ENV, process.cwd(), "");

const emailjsUserId = env.PUBLIC_EMAILJS_USER_ID;
const emailjsServiceId = env.PUBLIC_EMAILJS_SERVICE_ID;
const emailjsTemplateId = env.PUBLIC_EMAILJS_TEMPLATE_ID;

Remember to install vite if you're using pnpm:

pnpm install vite --save-dev
  1. Specific SSR Adapters: If you're using SSR and a specific adapter, you might need to use a specific method to access environment variables. For example, for the Deno adapter you have to use Deno.env.get(). This is mentioned in the Astro documentation (https://docs.astro.build/en/guides/environment-variables/).
#
const emailjsUserId = Deno.env.get('PUBLIC_EMAILJS_USER_ID');
const emailjsServiceId = Deno.env.get('PUBLIC_EMAILJS_SERVICE_ID');
const emailjsTemplateId = Deno.env.get('PUBLIC_EMAILJS_TEMPLATE_ID');

Please refer to the specific documentation for the adapter you're using for more details.

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