While working on Node.js + Express projects, I noticed I was repeatedly generating JWT secrets for my .env files.
Usually, I would either:
→ Search for an online secret generator
→ Run a long Node.js command manually
→ Copy the generated value
→ Paste it into .env
It's not difficult, but when you're doing it repeatedly, why not automate it?
So I created my own tiny Windows command called jwt.
Now I can simply type:
jwt
and get a secure random secret instantly.
Even better:
jwt 64
generates a 64-byte random secret instead of the default 32-byte secret.
🛠️ How I built it
I used Node.js' built-in crypto module, so there was no additional npm package involved.
First, I created a folder:
C:\Users\YOUR_USERNAME\bin
Then I created:
jwt.cmd
inside that folder.
The file contains:
@echo off
if "%~1"=="" (
set "bytes=32"
) else (
set "bytes=%~1"
)
node -e "console.log(require('crypto').randomBytes(%bytes%).toString('hex'))"
🔍 What does it do?
%~1 represents the first argument passed to the command.
So:
jwt
means no argument was provided, therefore:
bytes = 32
While:
jwt 64
sets:
bytes = 64
Then Node.js runs:
crypto.randomBytes(bytes)
which generates cryptographically secure random bytes.
Finally:
.toString('hex')
converts them into a hexadecimal string.
For example:
jwt
32 bytes → 64 hexadecimal characters
64 bytes → 128 hexadecimal characters
🌐 Making the command available everywhere
The final step was adding my bin folder to the Windows User PATH.
I went to:
Windows Search → Environment Variables → Edit the system environment variables → Environment Variables
Under User variables, I selected:
Path → Edit → New
and added:
C:\Users\YOUR_USERNAME\bin
After restarting the terminal, Windows could find jwt.cmd from any directory.
Now I can open CMD, PowerShell, or the VS Code terminal and simply run:
jwt
or:
jwt 64
💡 Why I like this
It's a very small automation, but it taught me something useful about Windows PATH, .cmd files, command-line arguments, and Node's crypto module.
More importantly, it removed a repetitive step from my development workflow.
Sometimes developer productivity isn't about building a huge tool.
Sometimes it's just:
"I do this repeatedly → let's automate it." ⚡
Top comments (0)