CLI app on Node.js How to pass the option value as an argument for the Shell command via child_process

How to pass the value of the –url option as an argument for the Wget command

#!/usr/bin/env node
'use strict';

const commander = require('commander');
const { exec } = require("child_process");
const program = new commander.Command();

  .option('-u, --url <value>', 'Website address');


const options = program.opts();

if (options.url) {
        exec("wget ${options.url}", (error, stdout, stderr) => {
            if (error) {
                console.log(`error: ${error.message}`);
            if (stderr) {
                console.log(`stderr: ${stderr}`);
            console.log(`stdout: ${stdout}`);


node app.js --url
error: Command failed: wget ${options.url}
/bin/bash: line 1: ${options.url}: bad substitution

It is necessary that the –url value is passed as an argument for wget. So that when you execute the node command app.js –url,she was doing wget

>Solution :

The issue is how you use string interpolation to format the wget command.
As mentioned here should be with backtick (`) characters instead of double quotes:

exec(`wget ${options.url}`, (error, stdout, stderr) => {

Leave a Reply