首页 > 解决方案 > 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?

标签: command-linedocumentationdocusaurus

解决方案


我创建了一个插件,将功能添加dotenv-webpack到 Docusaurus2 的 webpack 配置中。

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

您应该能够npm install docusaurus2-dotenv、启用systemvar并将其添加到您的插件部分,并且您的systemvar值将可以访问,例如process.env.PATH

这将允许您使用.env文件(如果您决定在将来使用它们),以及在 CI 期间创建或存在于构建代码的机器上的任何环境变量。

docusaurus.config.js

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


推荐阅读