Cannot read property 'toWIF' of undefined
First, note that HDNode was removed in 4.0.0, so you should use the BIP32 module you included. Second, you don't need the extra keyPair property, so your code becomes:

var bip32 = require('bip32')

var privKey = process.env.BIP32_PRIV;
var hdNode = bip32.fromBase58(privKey);

var count = process.env.GENERATE_ADDRESSES ? parseInt(process.env.GENERATE_ADDRESSES) : 1000; // how many addresses to watch


var rescan = 'false';

for (var i = 1; i &lt;= count; ++i) {
  console.log('bitcoin-cli importprivkey ' +  hdNode.derive(i).toWIF() + " '' " + rescan)
}