Execute command


how to execute a command without using the exec node but in a function ?
and recover the value


What type of command?

actually, I did not mention…

a command cli linux

Why do you not want to use the exec node?

because I want to execute commands before others, I prefer to do it in my function. my program will be cleaner

You could do it in a python script search on the flows site for one of the python nodes.

@baudetd to do it in your function node you would need to use the child_process module from the node.js runtime.

You will need to:

  1. Use the functionGlobalContext setting to make the module available to the function node. See the nodered.org docs on writing functions for details
  2. Read the node.js docs.on the child_process module and seek out examples.

Which is of course just what the exec node does for you....

const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', (data) => {
msg.payload = msg.payload + (stdout: ${data});

ls.stderr.on('data', (data) => {
msg.payload = msg.payload +(stderr: ${data});

ls.on('close', (code) => {
msg.payload = msg.payload +(child process exited with code ${code});

but i have an error with " functionGlobalContext"
i Don't understand that

"ReferenceError: require is not defined (line 0, col 84)"


I would like to run a omxplayer command and be able to kill it in a function
thanks for your help

or use the pre-built node ? https://flows.nodered.org/node/node-red-contrib-pi-omxplayer

no , i know this but i Don't use the prebuilt node

As I said, you need to read the documentation on writing functions to learn how to require additional modules. You cannot require them directly.


If anyone will find this thread wondering if the shell command can be executed in the function node, I'm posting a quick guide:

  1. Import child_process module to your function node:

  1. write your code :wink:
var child_process = childProcess.exec('echo Hello World!',(error, stdout, stderr) => {
  if (error) {
    node.warn(`exec error: ${error}`);
  node.warn(`stdout: ${stdout}`);
  //^^^new message can be send out instead of node.warn()^^^//
  msg1.stdout = `${stdout}`;
  node.warn(`stderr: ${stderr}`);
return msg1;

example flow:

[{"id":"ec878471ef7862aa","type":"inject","z":"866282f4.d0922","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":470,"y":1700,"wires":[["51ba29de9398140e"]]},{"id":"51ba29de9398140e","type":"function","z":"866282f4.d0922","name":"childProcess exec","func":"//documentation:\n//https://nodejs.org/api/child_process.html#child_processexeccommand-options-callback\nmsg1={};\nvar child_process = childProcess.exec('echo Hello World!',(error, stdout, stderr) => {\n  if (error) {\n    node.warn(`exec error: ${error}`);\n    return;\n  }\n  node.warn(`stdout: ${stdout}`);\n  /*\n  //^^^new message can be send out instead of node.warn()^^^//\n  msg1.stdout = `${stdout}`;\n  node.send(msg1);\n  */\n  node.warn(`stderr: ${stderr}`);\n});\nreturn msg1;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"childProcess","module":"child_process"}],"x":710,"y":1700,"wires":[["dde19cfc495b1608"]]},{"id":"dde19cfc495b1608","type":"debug","z":"866282f4.d0922","name":"childProcess exec","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":980,"y":1700,"wires":[]}]