๐ A Tiny Developer Tool I Built for My Node.js Workflow
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 .โฆ
While crafting Node.js and Express applications, I observed a repetitive occurrence of generating JWT secrets for my .env files. Typically, I would either: seek an online secret generator, execute a lengthy Node.js command manually, or copy and paste the generated value into my .env file. Although straightforward, this task seemed redundant when performed frequently.
Thus, I devised my own compact Windows command named jwt. Now, I merely need to type jwt to obtain a secure random secret instantly. An added feature allows jwt 64 to generate a 64-byte random secret instead of the default 32-byte secret.
To develop this tool, I utilized Node.js's built-in crypto module, eliminating the need for supplementary npm packages. Initially, I generated a directory: C:\Users\YOUR_USERNAME\bin, where I subsequently created a file titled jwt.cmd. The contents of this file included:
@echo off
if %~1 == (
set bytes=32
) else (
set bytes=%~1
)
node -e "console.log(require(crypto).randomBytes(%bytes%).toString('hex'))"
In this process, %~1 symbolizes the initial argument supplied to the command. Consequently, jwt indicates no argument was provided, thus: bytes = 32. Conversely, jwt 64 sets: bytes = 64. Subsequently, Node.js executes crypto.randomBytes(bytes), generating cryptographically secure random bytes. Finally, .toString('hex') transforms these bytes into a hexadecimal string. For instance, jwt 32 results in 64 hexadecimal characters, while jwt 64 yields 128 hexadecimal characters.
Lastly, I ensured the bin folder was accessible system-wide by adding it to the Windows User PATH. I navigated to Windows Search, Environment Variables, edited the system environment variables, selected User variables โ Path โ Edit โ New, and appended: C:\Users\YOUR_USERNAME\bin. After restarting my terminal, Windows could locate the jwt.cmd from any directory. Now, I can utilize CMD, PowerShell, or the VS Code terminal to simply execute: jwt or: jwt 64.
Although this may appear as a minor automation, it imparted valuable knowledge regarding Windows PATH, .cmd files, command-line arguments, and Node's crypto module. Significantly, it eliminated a repetitive task from my development workflow, illustrating that enhanced productivity often stems from automating mundane steps.
Written by urgent.news from Dev.to's reporting โ not their text. Machine-written โ may contain errors; check the original before relying on it.