I ended up doing something similar but I used settings.js (located in ~./.nodered) instead.
This allowed me to get the serial but also manipulate the full topic texts that I needed.
Since my node-red runs in systemd, it was becoming increasing difficult (at least to me, with my low knowledge of Linux) to create these different environment variables.
Here's my code that I inserted just before the module.exports line.
//get serial number of RPi from cpuinfo file
exec('cat /proc/cpuinfo | grep Serial | cut -d \' \' -f 2', (err, stdout, stderr) => {
if (err) {
//some err occurred
console.error(err);
} else {
// need to use slice to remove the newline character at the end of the returned text
process.env.SERIAL_NUMBER = stdout.slice(0,-1);
process.env.TOPIC_CONFIGURATION = "/configuration/" + stdout.slice(0,-1);
process.env.TOPIC_COMMANDS = "/commands/" + stdout.slice(0,-1);
}
});
// end of setting environment variables
Not the prettiest, but worked like a charm.