{"id":1691,"date":"2025-03-25T20:10:07","date_gmt":"2025-03-25T14:40:07","guid":{"rendered":"http:\/\/codeforgeek.com\/?p=1691"},"modified":"2025-03-25T20:37:40","modified_gmt":"2025-03-25T15:07:40","slug":"command-line-application-nodejs","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/command-line-application-nodejs\/","title":{"rendered":"Develop Command Line Applications Using Node.js &amp; SpeedTest.net"},"content":{"rendered":"\n<p>Node.js provides powerful utilities to develop command-line applications. Popular tools like <strong>Express Generator<\/strong> and <strong>Nodemon <\/strong>are examples of command-line applications built with Node.js. <\/p>\n\n\n\n<p>In this tutorial, we&#8217;ll learn how to use Node.js to create command-line utilities and build a sample internet speed test application using the <strong>speedtest.net API<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Getting Started with Command Line Applications<\/h2>\n\n\n\n<p>Command-line applications (CLIs) allow users to interact with programs through text commands in a terminal or console interface. Node.js makes it easy to create these applications with its built-in modules and npm ecosystem.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Hello World Command Line App:<\/h3>\n\n\n\n<p>Let&#8217;s start by creating a simple application that greets users by name. First, we&#8217;ll write code to print &#8220;Hello World&#8221; to the console:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.log(&quot;Hello World&quot;);\n<\/pre><\/div>\n\n\n<p>Save this code in a file named <strong>cli.js<\/strong> and run it using:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nnode cli.js\n<\/pre><\/div>\n\n\n<p>You&#8217;ll see &#8220;Hello World&#8221; printed in your console. But what if we want to pass a name as an argument? Let&#8217;s modify our code to accept input from the command line:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n#!\/usr\/bin\/env node\nconsole.log(&quot;Hello&quot;, process.argv&#x5B;2]);\n<\/pre><\/div>\n\n\n<p>The first line <strong>#!\/usr\/bin\/env node<\/strong> is a shebang that tells the system this script should be executed with Node.js. The <strong>process.argv<\/strong> array contains command-line arguments, with the third element (index 2) being the first user-provided argument.<\/p>\n\n\n\n<p>Now when you run:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nnode cli.js Shahid\n<\/pre><\/div>\n\n\n<p>It will output: <strong>Hello Shahid<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Making Script Globally Accessible:<\/h3>\n\n\n\n<p>To make your script accessible as a command from anywhere in your system, you need to create a <strong>package.json<\/strong> file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n{\n  &quot;name&quot;: &quot;HelloWorld&quot;,\n  &quot;version&quot;: &quot;1.0.0&quot;,\n  &quot;description&quot;: &quot;A simple greeting CLI&quot;,\n  &quot;author&quot;: &quot;&quot;,\n  &quot;license&quot;: &quot;ISC&quot;,\n  &quot;bin&quot;: {\n    &quot;sayHello&quot;: &quot;cli.js&quot;\n  },\n  &quot;dependencies&quot;: {}\n}\n<\/pre><\/div>\n\n\n<p>The <strong>bin <\/strong>field maps command names to the JavaScript files that should be executed when those commands are run. In this case, when users run <strong>sayHello<\/strong>, the system will execute<strong> cli.js<\/strong>.<\/p>\n\n\n\n<p>Install your package globally:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nnpm install -g\n<\/pre><\/div>\n\n\n<p>Now you can run your command from anywhere:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nsayHello Shahid\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Building Speedtest CLI Application<\/h2>\n\n\n\n<p>Now let&#8217;s create a more useful application: a command-line tool to test internet speed using the speedtest.net API.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setting Up the Project:<\/h3>\n\n\n\n<p>First, create a new directory for your project and initialize it:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nmkdir speedtest-cli\ncd speedtest-cli\nnpm init\n<\/pre><\/div>\n\n\n<p>Install the required dependencies:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nnpm install chalk progress speedtest-net --save\n<\/pre><\/div>\n\n\n<p>These packages will help us:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>speedtest-net<\/strong>: Wrapper for the speedtest.net API<\/li>\n\n\n\n<li><strong>progress<\/strong>: Creates progress bars in the terminal<\/li>\n\n\n\n<li><strong>chalk<\/strong>: Adds colors to terminal output<\/li>\n<\/ul>\n\n\n\n<p>Create a <strong>package.json <\/strong>file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n{\n  &quot;name&quot;: &quot;speedtest-cli&quot;,\n  &quot;version&quot;: &quot;1.0.0&quot;,\n  &quot;description&quot;: &quot;Command line tool to test internet speed&quot;,\n  &quot;main&quot;: &quot;index.js&quot;,\n  &quot;bin&quot;: {\n    &quot;speedtest&quot;: &quot;index.js&quot;\n  },\n  &quot;dependencies&quot;: {\n    &quot;chalk&quot;: &quot;^4.1.2&quot;,\n    &quot;progress&quot;: &quot;^2.0.3&quot;,\n    &quot;speedtest-net&quot;: &quot;^2.2.0&quot;\n  },\n  &quot;keywords&quot;: &#x5B;&quot;speedtest&quot;, &quot;cli&quot;, &quot;internet&quot;, &quot;speed&quot;],\n  &quot;author&quot;: &quot;&quot;,\n  &quot;license&quot;: &quot;ISC&quot;\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Creating Speedtest Application:<\/h3>\n\n\n\n<p>Now let&#8217;s write the main application code in <strong>index.js<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n#!\/usr\/bin\/env node\n\nconst speedTest = require(&#039;speedtest-net&#039;);\nconst ProgressBar = require(&#039;progress&#039;);\nconst chalk = require(&#039;chalk&#039;);\n\n\/\/ Create a progress bar utility function\nfunction createProgressBar(what) {\n  console.log(chalk.cyan(what));\n  return new ProgressBar(&#039;&#x5B;:bar] :percent&#039;, {\n    complete: &#039;=&#039;,\n    incomplete: &#039; &#039;,\n    width: 40,\n    total: 100\n  });\n}\n\nasync function runSpeedTest() {\n  try {\n    console.log(chalk.yellow(&#039;Starting speed test...&#039;));\n    console.log(chalk.dim(&#039;This may take up to 30 seconds\\n&#039;));\n\n    \/\/ Create progress bars\n    const downloadBar = createProgressBar(&#039;Testing download speed&#039;);\n    const uploadBar = createProgressBar(&#039;Testing upload speed&#039;);\n    \n    \/\/ Run the test\n    const result = await speedTest({\n      acceptLicense: true,\n      acceptGdpr: true,\n      progress: (progress) =&gt; {\n        if (progress.type === &#039;download&#039;) {\n          downloadBar.update(progress.percent \/ 100);\n        } else if (progress.type === &#039;upload&#039;) {\n          uploadBar.update(progress.percent \/ 100);\n        }\n      }\n    });\n\n    \/\/ Display results\n    console.log(&#039;\\n&#039; + chalk.green(&#039;\u2713&#039;) + &#039; Speed test completed!\\n&#039;);\n    console.log(chalk.cyan(&#039;Server:&#039;) + &#039; &#039; + result.server.name + &#039; (&#039; + result.server.location + &#039;, &#039; + result.server.country + &#039;)&#039;);\n    console.log(chalk.cyan(&#039;Ping:&#039;) + &#039; &#039; + result.ping.latency.toFixed(2) + chalk.dim(&#039; ms&#039;));\n    console.log(chalk.cyan(&#039;Jitter:&#039;) + &#039; &#039; + result.ping.jitter.toFixed(2) + chalk.dim(&#039; ms&#039;));\n    console.log(chalk.cyan(&#039;Download:&#039;) + &#039; &#039; + (result.download.bandwidth \/ 125000).toFixed(2) + chalk.dim(&#039; Mbps&#039;));\n    console.log(chalk.cyan(&#039;Upload:&#039;) + &#039; &#039; + (result.upload.bandwidth \/ 125000).toFixed(2) + chalk.dim(&#039; Mbps&#039;));\n    \n  } catch (error) {\n    console.error(chalk.red(&#039;Error running speed test:&#039;), error.message);\n    process.exit(1);\n  }\n}\n\n\/\/ Run the speed test\nrunSpeedTest();\n<\/pre><\/div>\n\n\n<p>This code:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Imports the required modules<\/li>\n\n\n\n<li>Creates a function to display progress bars<\/li>\n\n\n\n<li>Defines an async function to run the speed test<\/li>\n\n\n\n<li>Displays the results in a user-friendly format with colors<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Installing and Using Speedtest CLI:<\/h3>\n\n\n\n<p>Make the script executable:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nchmod +x index.js\n<\/pre><\/div>\n\n\n<p>Install it globally:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nnpm install -g\n<\/pre><\/div>\n\n\n<p>Now you can run your speed test from anywhere:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nspeedtest\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Advanced CLI Development Techniques<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Using Commander.js for Complex CLIs:<\/h3>\n\n\n\n<p>For more complex command-line applications, consider using the Commander.js library, which simplifies argument parsing and help documentation:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n#!\/usr\/bin\/env node\nconst { program } = require(&#039;commander&#039;);\nconst speedTest = require(&#039;speedtest-net&#039;);\nconst chalk = require(&#039;chalk&#039;);\n\nprogram\n  .version(&#039;1.0.0&#039;)\n  .option(&#039;-s, --server &lt;id&gt;&#039;, &#039;Use a specific server&#039;)\n  .option(&#039;-j, --json&#039;, &#039;Output results as JSON&#039;)\n  .option(&#039;-v, --verbose&#039;, &#039;Show detailed progress&#039;)\n  .parse(process.argv);\n\nconst options = program.opts();\n\nasync function runTest() {\n  \/\/ Implementation using options\n}\n\nrunTest();\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Adding Interactive Elements with Inquirer.js:<\/h3>\n\n\n\n<p>You can make your CLI more interactive using Inquirer.js:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst inquirer = require(&#039;inquirer&#039;);\n\nasync function promptUser() {\n  const answers = await inquirer.prompt(&#x5B;\n    {\n      type: &#039;list&#039;,\n      name: &#039;testType&#039;,\n      message: &#039;What would you like to test?&#039;,\n      choices: &#x5B;&#039;Download &amp; Upload&#039;, &#039;Download only&#039;, &#039;Upload only&#039;, &#039;Ping only&#039;]\n    }\n  ]);\n  \n  \/\/ Run the appropriate test based on user selection\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. Displaying Results with Tables:<\/h3>\n\n\n\n<p>For structured data, you can use the <strong>cli-table3 <\/strong>package:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst Table = require(&#039;cli-table3&#039;);\n\nfunction displayResults(results) {\n  const table = new Table({\n    head: &#x5B;&#039;Metric&#039;, &#039;Value&#039;, &#039;Unit&#039;],\n    colWidths: &#x5B;20, 10, 10]\n  });\n  \n  table.push(\n    &#x5B;&#039;Ping&#039;, results.ping.toFixed(2), &#039;ms&#039;],\n    &#x5B;&#039;Download&#039;, results.download.toFixed(2), &#039;Mbps&#039;],\n    &#x5B;&#039;Upload&#039;, results.upload.toFixed(2), &#039;Mbps&#039;]\n  );\n  \n  console.log(table.toString());\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>We&#8217;ve learned how to develop command-line applications using Node.js, from basic argument handling to creating a full-featured internet speed test tool. Command-line utilities are incredibly useful for automation, development workflows, and system administration tasks.<\/p>\n\n\n\n<p>The techniques covered in this tutorial can be applied to create various types of CLI applications, from simple scripts to complex tools with interactive features and rich output formatting.<\/p>\n\n\n\n<p><strong>For further exploration, consider checking out these libraries:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Commander.js &#8211;<\/strong> A complete solution for Node.js command-line interfaces<\/li>\n\n\n\n<li><strong>Inquirer.js &#8211;<\/strong> For creating interactive command prompts<\/li>\n\n\n\n<li><strong>Chalk &#8211; <\/strong>For terminal string styling<\/li>\n\n\n\n<li><strong>Ora &#8211; <\/strong>Elegant terminal spinners<\/li>\n\n\n\n<li><strong>Boxen &#8211; <\/strong>Create boxes in the terminal<\/li>\n<\/ul>\n\n\n\n<p>With these tools and techniques, you can create professional-grade command-line applications that enhance your productivity and simplify complex tasks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Node.js provides powerful utilities to develop command-line applications. Popular tools like Express Generator and Nodemon are examples of command-line applications built with Node.js. In this tutorial, we&#8217;ll learn how to use Node.js to create command-line utilities and build a sample internet speed test application using the speedtest.net API. Getting Started with Command Line Applications Command-line [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":33315,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[14],"tags":[],"class_list":["post-1691","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nodejs"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/03\/Develop-Command-Line-Applications.png",1000,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/03\/Develop-Command-Line-Applications-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/03\/Develop-Command-Line-Applications-300x180.png",300,180,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/03\/Develop-Command-Line-Applications-768x461.png",768,461,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/03\/Develop-Command-Line-Applications.png",1000,600,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/03\/Develop-Command-Line-Applications.png",1000,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/03\/Develop-Command-Line-Applications.png",1000,600,false]},"uagb_author_info":{"display_name":"Shahid","author_link":"https:\/\/codeforgeek.com\/author\/shahid\/"},"uagb_comment_info":0,"uagb_excerpt":"Node.js provides powerful utilities to develop command-line applications. Popular tools like Express Generator and Nodemon are examples of command-line applications built with Node.js. In this tutorial, we&#8217;ll learn how to use Node.js to create command-line utilities and build a sample internet speed test application using the speedtest.net API. Getting Started with Command Line Applications Command-line&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/1691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=1691"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/1691\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/33315"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=1691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=1691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=1691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}