{"id":30728,"date":"2016-05-04T12:13:13","date_gmt":"2016-05-04T06:43:13","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=30728"},"modified":"2016-05-04T14:57:08","modified_gmt":"2016-05-04T09:27:08","slug":"create-cli-in-nodejs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/create-cli-in-nodejs\/","title":{"rendered":"Create CLI in Nodejs"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">I wanted to create a command line tool for sharing files for some time now, but given all the things we have to go through to do that same was a hassle. But after <a title=\"Nodejs development\" href=\"http:\/\/www.tothenew.com\/mean-node-js-development-consulting\">getting to know Node.js<\/a> more, \u00a0I have never thought it would be done with so much ease. NPM helps us to make it an easy and time saving process. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">After creating and publishing my first CLI module , I want to share these simple steps to create your own CLI module. \u00a0In this blog, we will see how to make a node script do things on the command line.<\/span><\/p>\n<p><span style=\"line-height: 1.71429; font-size: 1rem;\"><strong><span style=\"font-weight: 400;\">We will also see an example of a<\/span><\/strong>\u00a0<\/span>file-share<span style=\"line-height: 1.71429; font-size: 1rem;\"> module.<\/span><\/p>\n<p><b>Step 1 : Node and NPM installation<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Here We assume that you have <\/span><b><i>NodeJs<\/i><\/b><span style=\"font-weight: 400;\"> and <\/span><b><i>NPM <\/i><\/b><span style=\"font-weight: 400;\">(Node package manager) installed on your computer. If not, please install both before we proceed further. <\/span><\/p>\n<p><span style=\"line-height: 1.71429; font-size: 1rem;\"><strong><span style=\"font-weight: 400;\">Installing <\/span><b><i>NodeJs<\/i><\/b><span style=\"font-weight: 400;\"> and <\/span><b><i>NPM <\/i><\/b><\/strong><i>is a simple process, <\/i>to do so <strong><span style=\"font-weight: 400;\">follow this<\/span><\/strong>\u00a0<\/span><a style=\"line-height: 1.71429; font-size: 1rem;\" title=\"link\" href=\"https:\/\/nodejs.org\/en\/download\/package-manager\/\">link<\/a><span style=\"line-height: 1.71429; font-size: 1rem;\">.<\/span><\/p>\n<p><b>Step 2 : Creating project and package.json configuration<\/b><\/p>\n<p>Now, create a project and run <strong>npm init<\/strong> .<\/p>\n<p><span style=\"font-weight: 400;\">NPM will ask you to enter some configuration values for your project. You can provide input or can choose to skip it by pressing <\/span><i><span style=\"font-weight: 400;\">enter<\/span><\/i> <i><span style=\"font-weight: 400;\">key. <\/span><\/i><span style=\"font-weight: 400;\">You can edit these entries anytime in future. It will create a template <\/span><i><span style=\"font-weight: 400;\">package.json <\/span><\/i><span style=\"font-weight: 400;\">file. <\/span><\/p>\n<p>NPM uses <em>package.json<\/em> file to know about your project and to install its dependencies.<\/p>\n<p>Now make following changes in <em>package.json<\/em> file :<\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">remove <\/span><i><span style=\"font-weight: 400;\">main<\/span><\/i><span style=\"font-weight: 400;\"> key.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">add <\/span><i><span style=\"font-weight: 400;\">preferGlobal<\/span><\/i><span style=\"font-weight: 400;\"> key and set it to <\/span><i><span style=\"font-weight: 400;\">true<\/span><\/i><span style=\"font-weight: 400;\">. Now this module will always be installed globally.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">add <\/span><i><span style=\"font-weight: 400;\">bin<\/span><\/i><span style=\"font-weight: 400;\"> object which maps commands to files. After installation, <\/span><i><span style=\"font-weight: 400;\">npm <\/span><\/i><span style=\"font-weight: 400;\">will map <\/span><i><span style=\"font-weight: 400;\">my-command<\/span><\/i><span style=\"font-weight: 400;\"> to index.js file.<\/span><\/li>\n<\/ul>\n<p>Example<em> package.json<\/em> :<\/p>\n<p>[js]<br \/>\n{<br \/>\n &quot;name&quot;: &quot;test-cli&quot;,<br \/>\n &quot;version&quot;: &quot;1.0.0&quot;,<br \/>\n &quot;description&quot;: &quot;&quot;,<br \/>\n &quot;scripts&quot;: {<br \/>\n &quot;test&quot;: &quot;echo \\&quot;Error: no test specified\\&quot; &amp;&amp; exit 1&quot;<br \/>\n },<br \/>\n &quot;author&quot;: &quot;&quot;,<br \/>\n &quot;license&quot;: &quot;ISC&quot;,<br \/>\n &quot;preferGlobal&quot; : true,<br \/>\n &quot;bin&quot; : {<br \/>\n &quot;my-command&quot; : &quot;index.js&quot;<br \/>\n }<br \/>\n}<br \/>\n[\/js]<\/p>\n<p><b>Step 3 : Creating index.js<\/b><\/p>\n<p>Now create<em> index.js<\/em> file and add the following code in it.<\/p>\n<p>[js]<br \/>\n#! \/usr\/bin\/env node<\/p>\n<p>console.log(&quot;my-command-cli running &#8230;&quot;);<br \/>\n[\/js]<\/p>\n<p><span style=\"font-weight: 400;\">(#!) at the start of first line also known as the <\/span><b>shebang, <\/b><span style=\"font-weight: 400;\">makes the script executable with what follows it.<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">\/bin\/env<\/span><\/i><span style=\"font-weight: 400;\"> looks at the current environment. We have added a \u201c<\/span><b>bin<\/b><span style=\"font-weight: 400;\">\u201d key in <\/span><i><span style=\"font-weight: 400;\">package.json <\/span><\/i><span style=\"font-weight: 400;\">file to tell Node where it can find the file to execute as a command (my-command). <\/span><\/p>\n<p><b>Step 4 : Installation and testing<\/b><\/p>\n<p>To install this module on your computer, run <em><strong>npm link<\/strong><\/em> .<\/p>\n<p>Run <em>my-command<\/em> in terminal\/command window to test your command and you will see the following output :<\/p>\n<p>[js]<br \/>\nmy-command-cli running &#8230;<br \/>\n[\/js]<\/p>\n<p>Now you can implement <em>index.js<\/em> as per your requirements.<\/p>\n<p><b>Use case : file-share module<\/b><\/p>\n<p>Following the above described approach, I have created and published a npm module named <a title=\"file-share\" href=\"https:\/\/www.npmjs.com\/package\/file-share\">file-share<\/a> .<\/p>\n<p>You can use it for sharing files and directories over LAN.<\/p>\n<p><a title=\"Github link\" href=\"https:\/\/github.com\/abhismcet005\/file-share\">Github link<\/a> \u00a0of file-share module.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to create a command line tool for sharing files for some time now, but given all the things we have to go through to do that same was a hassle. But after getting to know Node.js more, \u00a0I have never thought it would be done with so much ease. NPM helps us to [&hellip;]<\/p>\n","protected":false},"author":712,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1185],"tags":[3309,3310,3311],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/30728"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/712"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=30728"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/30728\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=30728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=30728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=30728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}