Questions about hugo environment variables

When I run hugo server I see the message in console Environment = 'development'. I am wondering if there is a way to access it using porcess.env.HUGO_ENV for example?

Or is it just to be used inside Hugo engine like if (eq hugo.Environment "development")?

In Windows I use command set HUGO_ENV=development&& hugo server. Then I am able to access it in postcss.config.js via process.env.HUGO_ENV. This works but it is not cross-platform friendly unless I use cross-env library or something like that.

You can pass one of these flags -e, --environment, followed by which environment you want
Also see

1 Like

I wish you would have queried me, if I was unable to make my question clear, instead of linking the whole documentation.

Not what I was looking for.

Thank you for your time.

He was trying to help (and he pointed you to the hugo command, not the “whole documentation”), be grateful and be polite.

So,

You can set it via either

  • HUGO_ENVIRONMENT os variable
  • --environment flag

You can get it with hugo.Environment. Note that the defaults are development for server and production for regular hugo.

4 Likes

When hugo server is running, is there a way to check the default (production) environment Hugo is on from the os?

To rephrase: Lets say hugo server is running. Will I be able to get process.env.HUGO_ENVIRONMENT === 'production' to be true in webpack or gulp?

I have concluded it is not possible. I started this post to check if i had missed something.

Use getenv

I apologise to both of you @zwbetz. I think I am being incomprehensible with my question.

Use getenv

getenv is a Hugo only command. I was looking to use something similar to getenv in webpack, gulp, postcss processes, while hugo server is running in a project…

Again, I’m sorry.

What I want can already be achieved by manually setting a custom variable in os.

// Windows example
set HUGO_ENV=development&& hugo server
// retrieve HUGO_ENV
process.env.HUGO_ENV === 'development'  // returns true

You are. I think if you explain what you want to do on a more general level, it may help.

1 Like

Found this thread searching for a similar answer.

hugo.Environment taps into that build mode:

{{ printf "%#v" hugo.Environment }}

Useful for development mode only partials.

We also have:

{{ if hugo.IsProduction }}
{{ end }}

(which uses hugo.Environment behind the scenes)

3 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.