Help with shutting down Raspberry Pi 3 with Blynk and NodeJS

I’ve borrowed this bit of code to shutdown the Pi gracefully through Blynk with NodeJS but I get the following error on the Pi when I use a button widget on Blynk which sends command on Virtual pin 1. I’ve been searching and reading for many hours but no luck on a solution for this noob. I’m running Nodejs version 6.11.3. Any help will be greatly appreciated.

This is the nodejs script:

var BlynkLib = require('blynk-library');

var AUTH = 'xxxxxxxxxxxxxxxxxxx';

var blynk = new BlynkLib.Blynk(AUTH);


var v1= new blynk.VirtualPin(1);

v1.on('write', function(){
 child = exec("sudo halt", function (error, stdout, stderr) {
 });});

Connects to Blynk server properly:

OnOff mode
Connecting to: blynk-cloud.com 8441
SSL authorization...
Connected
Authorized

Then I get this error when I press button in Blynk:

/home/pi/shutdownpi/pioff.js:11
 child = exec("sudo halt", function (error, stdout, stderr) {
         ^

ReferenceError: exec is not defined
    at .<anonymous> (/home/pi/shutdownpi/pioff.js:11:10)
    at emitOne (events.js:96:13)
    at emit (events.js:188:7)
    at Blynk.onReceive (/home/pi/shutdownpi/node_modules/blynk-library/blynk.js:462:27)
    at .<anonymous> (/home/pi/shutdownpi/node_modules/blynk-library/blynk.js:548:50)
    at emitOne (events.js:96:13)
    at emit (events.js:188:7)
    at TLSSocket.<anonymous> (/home/pi/shutdownpi/node_modules/blynk-library/blynk-node.js:212:16)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:188:7)

Try adding this declaration:

var exec = require('child_process').exec, child;

That did the trick! Thank you for responding right away.

hi! please do not forget to change the topic category as “solved” if you are happy with the solution.

thanks!