Hexo Environment Variables

| /

When we debug Hexo, we always encounter scenarios where we need to block some production environment logic. Such as some of Google’s JS libraries and so on.

So we need some way to distinguish debug environment, production environment, etc. What we probably need are environment variables.

In fact, Hexo also supports environment variables. Here are some ways to use them.

For example, we need an environment variable wqdebug to distinguish whether it is an offline environment or a production environment.

We can start Hexo like this

1
hexo s debug --wqdebug 1

We can print environment variable information like this

1
2
3
4
const test = <%- JSON.stringify(env) %>;

// The resulting value is
const test = {"args":{"_":[],"wqdebug":1},"debug":false,"safe":false,"silent":false,"env":"development","version":"6.2.0","cmd":"s","init":true};

So we can use this environment variable like this

1
2
<% if (!env.args["wqdebug"]) { %>
<% } %>

In addition, you may have noticed that Hexo’s own environment variables actually contain "env":"development" related information.

You can also use this environment variable directly.

Of course, you can also inject environment variables according to your needs, which will be more flexible.