DRAFT: Allow reloading of env variables before restarting nuxt dev server #466
+10
β1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
π Linked issue
Refer to nuxt/nuxt#28543 to reproduce this issue
β Type of change
π Description
When the contents of
.env
change, the dev server is unable to pick up the changes, even after the server restarts.There are multiple ways to go about implementing this feature, one of which is demonstrated in this PR.
c12
library is used to setup and load environment variables from.env
file. The following code is responsible for the same:cli/src/commands/dev.ts
Lines 47 to 52 in 34bdbbc
The function responsible for restarting the child process is:
cli/src/commands/dev.ts
Lines 167 to 187 in 34bdbbc
You'll notice that
process.env
is expanded directly, and there is no step to reload the environment variables from the changed.env
file.The change I proposed keeps track of the
cwd
anddotenv
args passed to thedev
command and then run this code prior to forking a new process:The reason for not using the same
setupDotenv
function provided byc12
is because if you look at the definition of the function (taken fromc12
's repo):Assume that an environment variable
NUXT_API_KEY
gets changed. Now the line marked with***
will check if the key is undefined in the target environment, i.e.process.env
, which in this case is actually defined. So it will never overwrite the value.I've marked this PR as draft because we can instead request upstream changes to
c12
to tidy up the code here.