command-linedocumentationdocusaurus

How to pass env var to Docusaurus v2


I've been trying to get environment variables to work in my documentation build.
I've had some success with adding a dotenv-webpack plugin and substituting values that way. This has the downside of needing a .env file of some kind

I would like to have my build know of environment variables automatically ie. everything that is output from printenv

I've tried adding this to package.json: TEST_ENV_VAR=working docusaurus start" But when I log the process.env object there is nothing there.

How can I make this work?


Solution

  • I created a plugin that adds the functionality of dotenv-webpack to Docusaurus2's webpack config.

    https://www.npmjs.com/package/docusaurus2-dotenv

    You should be able to npm install docusaurus2-dotenv, enable systemvar, and add it to your plugin section and your systemvar values will be accessible e.g. process.env.PATH.

    This would allow you to make use of .env files (if you decide want to use them in the future), as well as any environment variables that get created during CI or that exist on the machine that is building the code.

    docusaurus.config.js
    
    module.exports = {
      ..., // other docusaurus2 config
      plugins: [
        [
          "docusaurus2-dotenv",
          {
            systemvars: true,
          },
        ],
      ],
    }