-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Description
Discussed in #8549
Originally posted by miller-productions January 13, 2023
Hi folks,
I'm trying out using dotenv to dynamically set different values for url in the file docusaurus.config.js
For example...
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...
Then, I have separate dotenv files as such
// .env.development
DOCS_URL=http://localhost:3000
and
// .env.production
DOCS_URL=https://my-docs.someurl
Now, with this config setup, if I run npm start, everything works fine. NODE_ENV gets set to development, the relevant dotenv file gets loaded, all is good.
However, if I run npm run build, no joy. A quick check of console.log(process.env.NODE_ENV); inside docusaurus.config.js reveals that in this context, NODE_ENV is undefined.
Would anyone be able to assist? Am I missing something here?
Further info.
I made the following second attempt to check something...
I added a fallback value for that url config setting in case the dotenv config value can't be evaluated.
// load environment variables from relavent .env file
require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` })
console.log("process.env.NODE_ENV is: ", process.env.NODE_ENV);
...etc
const config = {
title: 'my title',
tagline: 'my tagline',
url: process.env.DOCS_URL || 'https://my-docs.someurl',
etc...
and I noticed that docusaurus.config.js appears to load twice, resulting in the following console output...
$ npm run build
> [email protected] build
> docusaurus build
process.env.NODE_ENV is: undefined
[INFO] [en] Creating an optimized production build...
process.env.NODE_ENV is: production
✔ Client
✔ Server
Compiled successfully in 18.64s
✔ Client
● Server █████████████████████████ cache (99%) shutdown IdleFileCachePlugin
stored
[SUCCESS] Generated static files in "build".
[INFO] Use `npm run serve` command to test your build locally.
So it looks like the first time through, process.env.NODE_ENV isn't set, which result in an error in node_modules\@docusaurus\core\lib\server\configValidation.js i.e. [ERROR] Error: "url" is required if you only rely on dotenv config alone, without the fallback as per my second attempt
Do you think this is a bug worth reporting?