如何移除React应用中未随Shell会话失效的临时环境变量?
Hey there! Let's tackle this issue where your REACT_APP_NOT_SECRET_CODE environment variable is sticking around even after you restart npm start. Here's what's going on and how to fix it:
Why is the variable still there?
When you ran ($env:REACT_APP_NOT_SECRET_CODE = "abcdef") -and (npm start) in PowerShell, you set the variable only for the current Shell session. If you didn't close that PowerShell window and just ran npm start again, the variable is still present in the active session—that's why Create React App keeps picking it up.
Step-by-Step Fixes
Remove the variable from your current PowerShell session
Run this command to delete the environment variable immediately in your open Shell:Remove-Item Env:REACT_APP_NOT_SECRET_CODEYou can verify it's gone by running:
echo $env:REACT_APP_NOT_SECRET_CODEIf this returns nothing, the variable is removed. Now restart
npm startand it won't see the variable anymore.Kill leftover Node.js processes (if needed)
Sometimes, npm might leave background processes running that still hold onto the environment variable. To clear these out:Stop-Process -Name node -ForceNote: This will terminate all running Node.js processes on your machine, so save any unsaved work in other Node apps first!
Check for accidental permanent setup
If the variable keeps coming back even after closing and reopening PowerShell, double-check if you accidentally added it to your PowerShell profile. Open your profile file with:notepad $PROFILELook for any lines setting
REACT_APP_NOT_SECRET_CODE—delete them if found, save the file, and restart PowerShell.
That should take care of the lingering variable!
内容的提问来源于stack exchange,提问作者suhaas.dev

