Master Node.js v24: Real‑Time Watch, Multi‑File Test, Native Env Files & Colorful Console
With Node.js v24 released, this guide demonstrates how to use the new --watch flag for live file monitoring, the --test command for multi‑file testing, native --env-file support for environment variables, colorful console output via util.styleText, and built‑in WebSocket client/server examples.
Introduction
Node.js v18 reached end‑of‑life on April 30, and Node.js v24 was released on May 6. This article explores practical features of the new version, providing hands‑on examples for developers who want to adopt the latest capabilities.
1. Real‑Time File Watching with --watch
Use the node --watch <filename> command to automatically re‑run a script whenever the file is saved, similar to the Live Server experience.
node --watch app.js2. Multi‑File Testing with --test
The --test flag allows you to run multiple test files in a single command. node --test test1.js test2.js Running the command on a correct script produces the expected output, while running it on a script with errors shows the failure details.
3. Native Environment Variable Support with --env-file
Node.js v20.6+ can read a .env file without external libraries. Create a .env file with key‑value pairs and invoke: node --env-file .env app.js This loads the variables directly into process.env. The article notes that earlier attempts may fail unless the correct syntax is used.
4. Colorful Console Output
The built‑in util module provides styleText(style, text) for styled console logs. Example of nested styling:
const { styleText } = require('util');
console.log(styleText('italic', styleText('bold', styleText('blue', 'Blue bold italic'))));A more elaborate helper function generates random background colors, emojis, and styled text. The function selects random style keywords from arrays and prints the result with console.log(styleText(selectedStyle, content)).
Sample Helper Function
function co(text) {
const colorArr = ['bgBlack','bgRed','bgGreen','bgYellow','bgBlue','bgMagenta','bgCyan','bgWhite'];
const fontColorArr = ['black','red','green','yellow','blue','magenta','cyan','white'];
const emojiArr = ['🌰','🌱','🌲','🌳','🌴','🌵','🌷','🌸','🌹','🌺'];
const i = Math.floor(Math.random() * fontColorArr.length);
const j = Math.floor(Math.random() * emojiArr.length);
const bg = fontColorArr[i];
const content = `${emojiArr[j].repeat(3)}${text}`;
console.log(styleText(bg, content));
}5. Built‑in WebSocket Support
Node.js now includes a native WebSocket implementation. Below are minimal server and client examples.
Server
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', (ws) => {
console.log('New client connected');
ws.on('message', (message) => {
console.log('Received:', message.toString());
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(`Server received: ${message}`);
}
});
});
ws.on('close', () => console.log('Client disconnected'));
});
console.log('WebSocket server running at ws://localhost:8080');Client
// Native WebSocket (Node.js 22+)
const ws = new WebSocket('ws://localhost:8080');
ws.addEventListener('open', () => {
console.log('Connected to server');
ws.send('Hello, server!');
});
ws.addEventListener('message', ({ data }) => {
console.log('Server says:', data.toString());
});
ws.addEventListener('close', () => console.log('Connection closed'));Appendix: Installing Node.js v24
The installer includes the runtime (≈3102 KB), npm (≈24 KB), npx (≈1116 KB), shortcuts to documentation, and updates the system PATH for global package installation.
After installation, verify versions with:
node -v
npm -vNo system reboot is required for the changes to take effect.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
