DEV Community

Cover image for [HUGO]: How to use variables from .env
Anastasiia_Berest
Anastasiia_Berest

Posted on • Edited on

3

[HUGO]: How to use variables from .env

The initial task you need to accomplish on Hugo is how to add variables from .env to JavaScript when building the site using hugo server or hugo --gc --minify after deploying on Netlify.
And what I understand after researching for this:

  • touch .env file and add variables with names that start with HUGO_PARAMS_ as required read

  • Add to config.toml

[security]
  enableInlineShortcodes = false
  [security.funcs]
    getenv = ['^HUGO_']
Enter fullscreen mode Exit fullscreen mode
  • Add to Netlify Deploy Environment variables from .env

  • Add to \layouts_default\baseof.html something like this

<script>
const apiKey = "{{ getenv "HUGO_PARAMS_FIREBASE_API_KEY" }}";
//console.log('apiKey', apiKey);
</script>
Enter fullscreen mode Exit fullscreen mode
  • Deploy that That's all, it will be a success.

To run it locally, you will need to write all the variables in the following format:

env HUGO_TITLE="Some Title" hugo server
Enter fullscreen mode Exit fullscreen mode

(I couldn't find any other way, and it's terribly inconvenient)
read


Sources: 1 2 3 4

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs