You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is regarding how the current @next/env has some issues, and at the same time the headache I had with how there was little to nothing documented on the main webpage - unfortunately, there's no way to do general discussions here, so I've added this to the "Help" category.
The "default" convention of environment variables is:
Assuming that environment will always be development by default
If provided through NODE_ENV, it can be over-ridden to development (default), production, staging and test
At present, @next/env loads environment variables in a non-conventional way.
constmode=isTest ? 'test' : dev ? 'development' : 'production'
constdotenvFiles=[
`.env.${mode}.local`,
// Don't include `.env.local` for `test` environment
// since normally you expect tests to produce the same
// results for everyone
mode!=='test'&&`.env.local`,
`.env.${mode}`,
'.env',
].filter(Boolean)asstring[]
The issue with the code is that:
a) Line 136 check if process.env.NODE_ENV is 'test', line 137 sets mode to 'test' if it is 'test' ,and line 143 checks if it is 'test'. isTest and mode is used nowhere in the entire file - all of this could've been done by just removing line 136 and 137, and replacing ${mode} with ${process.env.NODE_ENV}
b) the value of dev will most probably be set to something like:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
This is regarding how the current
@next/env
has some issues, and at the same time the headache I had with how there was little to nothing documented on the main webpage - unfortunately, there's no way to do general discussions here, so I've added this to the "Help" category.The "default" convention of environment variables is:
NODE_ENV
, it can be over-ridden to development (default), production, staging and testAt present,
@next/env
loads environment variables in a non-conventional way.next.js/packages/next-env/index.ts
Lines 136 to 146 in 126c226
The issue with the code is that:
a) Line 136 check if
process.env.NODE_ENV
is 'test', line 137 setsmode
to 'test' if it is 'test' ,and line 143 checks if it is 'test'.isTest
andmode
is used nowhere in the entire file - all of this could've been done by just removing line 136 and 137, and replacing${mode}
with${process.env.NODE_ENV}
b) the value of dev will most probably be set to something like:
by making the changes in point (a) and letting next/env do the decision for us by reading the
NODE_ENV
value, there is no need to check for the samec) there isn't any way to provide a staging env, the changes provided in point (a) should be able to resolve this
We may also be able to remove dotenv, since NodeJS 20 and onwards supports env natively, and dotenv-expand may be accomplished through destructing.
Additional information
Example
-
Beta Was this translation helpful? Give feedback.
All reactions