Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NODE_ENV not set initially prior to configValidation for 'npm run build' #8599

Closed
Josh-Cena opened this issue Jan 30, 2023 Discussed in #8549 · 2 comments · Fixed by #8677
Closed

NODE_ENV not set initially prior to configValidation for 'npm run build' #8599

Josh-Cena opened this issue Jan 30, 2023 Discussed in #8549 · 2 comments · Fixed by #8677
Labels
bug An error in the Docusaurus core causing instability or issues with its execution

Comments

@Josh-Cena
Copy link
Collaborator

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

> docs-website@0.0.0 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?

@Josh-Cena Josh-Cena added the bug An error in the Docusaurus core causing instability or issues with its execution label Jan 30, 2023
@Josh-Cena
Copy link
Collaborator Author

As mentioned in #8549 (comment), I think we should make sure all commands have a NODE_ENV. Maybe we should default to development.

@slorber
Copy link
Collaborator

slorber commented Feb 17, 2023

As mentioned in #8549 (comment), I think we should make sure all commands have a NODE_ENV. Maybe we should default to development.

Implemented in #8677

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution
Projects
None yet
2 participants