Running a Node.js Application with Temporary Environment Variables in PowerShell
Overview
When developing a Node.js application locally, you often need to:
- Stop any previously running Node.js processes.
- Set environment variables (such as passwords or secrets).
- Start the application with the new configuration.
This guide explains each step.
Step 1: Stop All Running Node.js Processes
Get-Process node -ErrorAction SilentlyContinue | Stop-Process -Force
What this command does
| Part | Description |
|---|---|
Get-Process node |
Finds all running Node.js processes. |
-ErrorAction SilentlyContinue |
Prevents PowerShell from showing an error if no Node.js process is running. |
Stop-Process -Force |
Immediately terminates all matching Node.js processes. |
Why use it?
Sometimes an old Node.js process is still running in the background and holding a port (for example, port 3000 or 8080). Stopping it ensures you start with a clean environment.
Step 2: Set Environment Variables
$env:ADMIN_PASSWORD = "dev-password"
$env:SESSION_SECRET = "dev-secret"
What are Environment Variables?
Environment variables are values provided to your application outside of your source code.
Instead of writing secrets directly into your code:
❌ Bad
const password = "dev-password";
Use environment variables:
✅ Good
const password = process.env.ADMIN_PASSWORD;
This keeps sensitive information separate from your application code.
Understanding Each Variable
ADMIN_PASSWORD
$env:ADMIN_PASSWORD = "dev-password"
Your application can read this value as:
process.env.ADMIN_PASSWORD
Example usage:
if (inputPassword === process.env.ADMIN_PASSWORD) {
console.log("Access granted");
}
SESSION_SECRET
$env:SESSION_SECRET = "dev-secret"
This secret is commonly used for:
- Signing cookies
- Signing JWT tokens
- Encrypting sessions
- Verifying authentication data
Example:
const secret = process.env.SESSION_SECRET;
Step 3: Verify the Variables (Optional)
You can check that the variables were set correctly.
echo $env:ADMIN_PASSWORD
Output
dev-password
Likewise,
echo $env:SESSION_SECRET
Output
dev-secret
Step 4: Start Your Application
After setting the variables, run your application in the same PowerShell window.
Example:
npm run dev
or
node app.js
Your application will now have access to both environment variables.
Important Note
Environment variables created like this are temporary.
They exist only for the current PowerShell session.
If you:
- Close PowerShell
- Open a new terminal
- Restart your computer
you must set them again.
Complete Workflow
# Stop any running Node.js process
Get-Process node -ErrorAction SilentlyContinue | Stop-Process -Force
# Set development environment variables
$env:ADMIN_PASSWORD = "dev-password"
$env:SESSION_SECRET = "dev-secret"
# Start the application
npm run dev
Best Practices
✅ Store secrets in environment variables instead of hardcoding them.
✅ Use different values for development, testing, and production.
✅ Never commit real passwords or secrets to Git.
✅ Consider using a .env file with a library such as dotenv for local development.
Example .env file:
ADMIN_PASSWORD=dev-password
SESSION_SECRET=dev-secret
Summary
In this tutorial, you learned how to:
- Stop existing Node.js processes.
- Create temporary environment variables in PowerShell.
- Access those variables from a Node.js application.
- Start the application with the new configuration.
- Follow basic security best practices for managing secrets during development.