在Windows执行HOST=localhost npm run start:debug报错的解决咨询
HOST=localhost npm run start:debug Error on Windows Hey there! This is a super common gotcha when switching between macOS/Linux shells and Windows' CMD/PowerShell—they handle temporary environment variables totally differently. Let's get your server running smoothly on both Windows shells.
Why the Original Command Fails
On macOS/Linux, you can prefix a command with VAR=value to set a temporary environment variable just for that one command. But Windows' CMD and PowerShell don't recognize this syntax, which is why you're seeing those "not recognized" errors.
Solution for Windows CMD
In CMD, you need to use the set command to define the environment variable first, then chain the npm command with &&:
set HOST=localhost && npm run start:debug
set HOST=localhostsets the temporary environment variable for your current CMD session.&&ensures the npm command only runs if thesetcommand succeeds.
Solution for Windows PowerShell
PowerShell uses its own environment variable syntax with $env:. Use this command instead:
$env:HOST="localhost"; npm run start:debug
$env:HOST="localhost"sets the temporary variable for your current PowerShell session.- The semicolon (
;) separates the two commands so they run in sequence.
Bonus: Cross-Platform Alternative (No Shell-Specific Commands!)
If you want to avoid remembering different commands for each system, create a .env file in your project root with this line:
HOST=localhost
Most modern frameworks (like React, Vue, or Node.js projects using dotenv) automatically load variables from .env files. Now you can just run npm run start:debug on any system—no extra environment variable setup needed!
内容的提问来源于stack exchange,提问作者Natansh Prasad

