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,
      },
    ],
  ],
}

Answer from Jonny Nabors on Stack Overflow
Author: rickymarcon
🌐
npm
npmjs.com › package › docusaurus2-dotenv
docusaurus2-dotenv - npm
A Docusaurus2 plugin that supports dotenv and other environment variables and only exposes what you choose and use. Latest version: 1.4.0, last published: 6 years ago. Start using docusaurus2-dotenv in your project by running `npm i ...
      » npm install docusaurus2-dotenv
    
Published: May 08, 2020
Version: 1.4.0
🌐
Docusaurus
docusaurus.io › guides › deployment
Deployment | Docusaurus
July 10, 2026 - docusaurus.config.js · // If you are using dotenv (https://www.npmjs.com/package/dotenv) import 'dotenv/config'; export default { title: '...', url: process.env.URL, // You can use environment variables to control site specifics as well · customFields: { // Put your custom environment here ·
Top answer
1 of 8
13

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,
      },
    ],
  ],
}

2 of 8
10

Build time environment variables

There are two steps:

  • Run npm i --save-dev dotenv
  • In your docusaurus.config.js, just add:
require('dotenv').config()
  • Confirm your .env directory contains environment variables, e.g.
ENVIRONMENT_VARIABLE_1=hello_there

Your .env file will be loaded, and you can use process.env.ENVIRONMENT_VARIABLE_1 now.

Runtime environment variables:

To use process.env variables in React components for example, do the builtime environment variables steps above, then use the customFields field of the docusaurus config object:

const config = {
  ...
  customFields: {
    'ENVIRONMENT_VARIABLE_1': process.env.ENVIRONMENT_VARIABLE_1,
    'ENVIRONMENT_VARIABLE_2': process.env.ENVIRONMENT_VARIABLE_2,
  },
  ...
}

and in my typescript component, access them with:

const {siteConfig} = useDocusaurusContext();

  return <div>{`${siteConfig.ENVIRONMENT_VARIABLE_1}`}</div>;

Read Custom Configurations in the docusaurus documentation for more information.

Comment

Jonny Nabors's answer (and package) was unnecessary for me and actually confused me. If you want your build process to use your environment variables, use the extremely popular npm package that has been downloaded 22 million times this week (dotenv, rather than his package (docusaurus2-dotenv), which did not work for me.

Perhaps his package is more useful if you needed to use the environment variables at runtime whilst avoiding adding it to the configuration object like I did above? However, in that case, I also found another solution, which is to use environment variables beginning with REACT_APP_.

🌐
GitHub
github.com › jonnynabors › docusaurus2-dotenv
GitHub - jonnynabors/docusaurus2-dotenv · GitHub
A Docusaurus2 plugin that supports dotenv and other environment variables and only exposes what you choose and use
Starred by 19 users
Forked by 3 users
Languages: TypeScript
🌐
npm
npmjs.com › package › docusaurus-plugin-dotenv
docusaurus-plugin-dotenv - npm
May 24, 2021 - The docusaurus-plugin-dotenv plugin uses dotenv-webpack to enable Dotenv support in Docusaurus2+ projects.
      » npm install docusaurus-plugin-dotenv
    
Published: May 24, 2021
Version: 1.0.1
🌐
npm
npmjs.com › package › docusaurus2-dotenv-2
docusaurus2-dotenv-2 - npm
October 1, 2022 - A Docusaurus2 plugin that supports dotenv and other environment variables and only exposes what you choose and use. Latest version: 1.4.1, last published: 4 years ago. Start using docusaurus2-dotenv-2 in your project by running `npm i ...
      » npm install docusaurus2-dotenv-2
    
Published: Oct 01, 2022
Version: 1.4.1
🌐
Read the Docs
docs.readthedocs.com › platform › latest › intro › docusaurus.html
Deploying Docusaurus on Read the Docs — Read the Docs user documentation
Set your Docusaurus url to your Read the Docs canonical URL using dotenv and a Read the Docs environment variable:
🌐
Libraries.io
libraries.io › npm › docusaurus2-dotenv
docusaurus2-dotenv 1.4.0 on npm - Libraries.io - security & maintenance data for open source software
A Docusaurus2 plugin that supports dotenv and other environment variables and only exposes what you choose and use - 1.4.0 - a TypeScript package on npm
Find elsewhere
Author: rickymarcon
🌐
Snyk
snyk.io › snyk vulnerability database › npm
docusaurus2-dotenv vulnerabilities | Snyk
Further analysis of the maintenance status of docusaurus2-dotenv based on released npm versions cadence, the repository activity, and other data points determined that its maintenance is Inactive.
🌐
CodeSandbox
codesandbox.io › examples › package › docusaurus2-dotenv
docusaurus2-dotenv examples - CodeSandbox
AboutA Docusaurus2 plugin that supports dotenv and other environment variables and only exposes what you choose and use2,475Weekly Downloads
🌐
Onatim
onatim.com › docusaurus › environment variables
Environment variables - Onat | Developer
//... const lightCodeTheme = require("prism-react-renderer/themes/github") const darkCodeTheme = require("prism-react-renderer/themes/dracula") require("dotenv").config() /** @type {import('@docusaurus/types').Config} */ const config = { title: "Developer", @ -24,6 +26,14 @@ const config = { onBrokenLinks: "throw", onBrokenMarkdownLinks: "warn", // customFields are used to pass env variables to Docusaurus' useDocusaurusContext // hook which is used for importing env variables in the components customFields: { EMAILJS_SERVER_ID: process.env.EMAILJS_SERVER_ID, EMAILJS_TEMPLATE_ID: process.env.EMAILJS_TEMPLATE_ID, EMAILJS_PUBLIC_KEY: process.env.EMAILJS_PUBLIC_KEY, }, i18n: { defaultLocale: "en", locales: ["en"], }, //... somepage.tsx ·
🌐
npm
npmjs.com › package › @telus-uds › docusaurus-plugin-webpack-config
@telus-uds/docusaurus-plugin-webpack-config - npm
January 31, 2025 - Plugin designed to integrate webpack configurations into UDS Docusaurus project. It simplifies the management of webpack settings, allowing to optimize and tailor the docusaurus build to specific needs.
      » npm install @telus-uds/docusaurus-plugin-webpack-config
    
Published: Jan 31, 2025
Version: 2.0.1
Author: TELUS Digital
🌐
Docusaurus.community
docusaurus.community › plugindirectory
Docusaurus Plugin Directory | Docusaurus.community
A Docusaurus 2 plugin that supports dotenv and other environment variables and only exposes what you choose and use.
🌐
The DAX Shepherd
thedaxshepherd.com › home › using environment variables in docusaurus
Using Environment Variables in Docusaurus
April 1, 2026 - For instance, if you implemented a commenting system in Docusaurus, instead of going to the source files to change something such as the theme you can manage that change from the configuration file. It is giving you the same functionality that other built-in variables in the configuration file give you such as a central place to change the Google Analytics ID or the website title. We will be using the module Dotenv to load the .env file into process.env.
🌐
jsDelivr
jsdelivr.com › package › npm › docusaurus2-dotenv-2
docusaurus2-dotenv-2 CDN by jsDelivr - A CDN for npm and GitHub
October 1, 2022 - A Docusaurus2 plugin that supports dotenv and other environment variables and only exposes what you choose and use
Published: Oct 01, 2022
🌐
GitHub
github.com › facebook › docusaurus › issues › 8599
NODE_ENV not set initially prior to configValidation for 'npm run build' · Issue #8599 · facebook/docusaurus
January 30, 2023 - In docusaurus.config.js, I have the following relevant lines... // load environment variables from relavent .env file require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }) ...etc const config = { title: 'my title', tagline: 'my tagline', url: process.env.DOCS_URL, etc...
Author: facebook
🌐
Npm
npm.io › package › docusaurus2-dotenv
Docusaurus2-dotenv NPM | npm.io
A Docusaurus2 plugin that supports dotenv and other environment variables and only exposes what you choose and use