{"id":88175,"date":"2023-08-28T19:07:09","date_gmt":"2023-08-28T13:37:09","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=88175"},"modified":"2023-08-28T19:07:09","modified_gmt":"2023-08-28T13:37:09","slug":"node-js-command-line-options","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/","title":{"rendered":"Node.js Command Line Options"},"content":{"rendered":"<h2>Node JS command line<\/h2>\n<p>One of its notable features is the ability to accept command line options, which provide developers with a range of configuration and customization possibilities. In this article, we will explore the various command line options available in Node.js and learn how to use them effectively.<\/p>\n<h3>Command Line Options in Node.js:<\/h3>\n<p>Node.js provides a set of built-in command line options that you can use when running your JavaScript files. These options enable you to control various aspects of the Node.js runtime environment and tweak its behaviour according to your specific needs.<\/p>\n<h4>Let&#8217;s look at some commonly used command line options:<\/h4>\n<p><strong>1. &#8211;help:<\/strong> This option displays the available command line options and their descriptions.<\/p>\n<p><strong>2. &#8211;version:<\/strong> It prints the version of Node.js currently installed on your system.<\/p>\n<p><strong>3. &#8211;inspect:<\/strong> This option enables the built-in Node.js debugger, allowing you to connect to it using a debugger client like Chrome DevTools.<\/p>\n<p><strong>4. &#8211;inspect-brk:<\/strong> Similar to &#8211;inspect, this option starts the debugger and pauses the execution of your script at the beginning so you can debug from the very start.<\/p>\n<p><strong>5. &#8211;max-old-space-size:<\/strong> Use this option to increase the maximum memory limit available to your Node.js process. For example, &#8211;max-old-space-size=4096 sets the maximum heap size to 4GB.<\/p>\n<p><strong>6. &#8211;trace-deprecation:<\/strong> It logs a stack trace whenever a deprecated API is used, helping you identify and update outdated code.<\/p>\n<p><strong>7. &#8211;trace-warnings:<\/strong> This option logs stack traces for certain runtime warnings, such as deprecations, promise rejections, and more.<\/p>\n<p><strong>8. &#8211;experimental modules:<\/strong> Enables experimental support for ECMAScript modules (ESM). Note that this feature is still experimental and subject to change.<\/p>\n<h3>Using Command Line Options in Node.js:<\/h3>\n<p>To use these command line options, you need to open a terminal or command prompt and navigate to the directory where your JavaScript file is located. Then, you can run your script with the desired options.<\/p>\n<p><strong>Here&#8217;s an example of using the &#8211;inspect option to enable the Node.js debugger:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">node --inspect myscript.js\n<\/pre>\n<p>In this example, \u2018myscript.js\u2019 is the name of your JavaScript file. Once the script is running, you can open the Chrome browser and navigate to \u2018chrome:\/\/inspect\u2019 to connect to the Node.js debugger.<\/p>\n<p>Similarly, you can use other command line options by appending them to the \u2018node\u2019 command when running your script.<\/p>\n<p>It&#8217;s worth noting that you can also pass additional arguments to your script after the command line options.<\/p>\n<p><strong>For example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">node --max-old-space-size=4096 myscript.js arg1 arg2<\/pre>\n<p>In this case, \u2018arg1\u2019 and \u2018arg2\u2019 are arguments that will be accessible within your script via the \u2018process.argv\u2019 array.<\/p>\n<h3>Command Line Arguments:<\/h3>\n<p>Node.js allows you to pass command line arguments to your script. These arguments can be accessed using the \u2018process.argv\u2019 array. The first element of the array (\u2018process.argv[0]\u2019) contains the path to the Node.js executable, and the second element (\u2018process.argv[1]\u2019) contains the path to the executed file. Any additional arguments start from \u2018process.argv[2]\u2019 onwards.<\/p>\n<p><strong> For example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/ app.js\nconsole.log(process.argv);\n<\/pre>\n<p><strong>Running the following command:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">node app.js arg1 arg2 arg3\n<\/pre>\n<p><strong>Will produce the following output:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">[ '\/usr\/local\/bin\/node', '\/path\/to\/app.js', 'arg1', 'arg2', 'arg3' ]\n<\/pre>\n<h3>Global Variables:<\/h3>\n<p>Node.js provides a set of global variables that can be used in your scripts. These variables are accessible without requiring any additional imports.<\/p>\n<p><strong> Some commonly used global variables include:<\/strong><\/p>\n<p><strong>\u2018__dirname\u2019:<\/strong> The directory name of the current module.<\/p>\n<p><strong>\u2018__filename\u2019:<\/strong> The file name of the current module.<\/p>\n<p><strong>\u2018require\u2019:<\/strong> A function to include and use external modules.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/ app.js\nconsole.log(__dirname);\nconsole.log(__filename);\nconsole.log(require);\n<\/pre>\n<h4>Environment Variables:<\/h4>\n<p>Node.js allows you to access environment variables using the \u2018process.env\u2019 object. These variables are set outside of your application and can be useful for storing configuration values or sensitive information. To access an environment variable, use \u2018process.env.VARIABLE_NAME\u2019.<\/p>\n<p><strong>For example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/ app.js\nconsole.log(process.env.HOME);\nconsole.log(process.env.NODE_ENV);\n<\/pre>\n<h3>Custom Command Line Options:<\/h3>\n<p>In addition to the built-in options, you can define custom command line options for your Node.js applications using libraries like \u2018yargs\u2019 or \u2018commander\u2019. These libraries provide a convenient way to define and parse command line options with minimal effort.<\/p>\n<p>Here&#8217;s an example using the \u2018yargs\u2019 library:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/ app.js\nconst argv = require('yargs')\n  .option('name', {\n    alias: 'n',\n    description: 'Your name',\n    type: 'string',\n    demandOption: true\n  })\n  .option('age', {\n    alias: 'a',\n    description: 'Your age',\n    type: 'number',\n    demandOption: true\n  })\n  .argv;\n \nconsole.log(`Hello ${argv.name}! You are ${argv.age} years old.`);\n<\/pre>\n<p><strong>Running the following command:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">node app.js --name John --age 25\n<\/pre>\n<p><strong>Will produce the following output:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Hello John! You are 25 years old.\n<\/pre>\n<h3>Conclusion<\/h3>\n<p>Node.js command line options provide developers with a range of configuration and debugging capabilities. Whether you need to enable the debugger, increase memory limits, or log warnings and deprecations, these options allow you to fine-tune the Node.js runtime environment according to your requirements.<\/p>\n<p>Harnessing the power of command line options in Node.js can greatly enhance your development workflow and help you build robust and efficient applications. So, go ahead, experiment with different options, and leverage the flexibility they provide to take your Node.js projects to the next level.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Node JS command line One of its notable features is the ability to accept command line options, which provide developers with a range of configuration and customization possibilities. In this article, we will explore&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":88177,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4984],"tags":[5158,5159,5146,5160,5161],"class_list":["post-88175","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js-tutorials","tag-command-line","tag-command-line-in-node-js","tag-node-js","tag-node-js-command-line","tag-node-js-command-line-options"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Node.js Command Line Options - TechVidvan<\/title>\n<meta name=\"description\" content=\"In this article, we will explore the various command line options available in Node.js and learn how to use them effectively.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Node.js Command Line Options - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"In this article, we will explore the various command line options available in Node.js and learn how to use them effectively.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-28T13:37:09+00:00\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Node.js Command Line Options - TechVidvan","description":"In this article, we will explore the various command line options available in Node.js and learn how to use them effectively.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/","og_locale":"en_US","og_type":"article","og_title":"Node.js Command Line Options - TechVidvan","og_description":"In this article, we will explore the various command line options available in Node.js and learn how to use them effectively.","og_url":"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2023-08-28T13:37:09+00:00","author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Node.js Command Line Options","datePublished":"2023-08-28T13:37:09+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/"},"wordCount":752,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/#primaryimage"},"thumbnailUrl":"","keywords":["Command Line","Command Line in Node.js","Node.js","node.js command line","node.js command line options"],"articleSection":["Node Js Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/","url":"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/","name":"Node.js Command Line Options - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/#primaryimage"},"thumbnailUrl":"","datePublished":"2023-08-28T13:37:09+00:00","description":"In this article, we will explore the various command line options available in Node.js and learn how to use them effectively.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/node-js-command-line-options\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Node.js Command Line Options"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/88175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=88175"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/88175\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=88175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=88175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=88175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}