Breaking Out of the Browser, Part 1.0: Command Line Utility

Your package.json file should look something like this:All right, this all looks pretty good, but if you try to run say-hello from the terminal, it’s still not going to work!That’s because our operating system doesn’t yet know anything about a say-hello command, and it doesn’t automatically know to look in Hello-World’s package.json file to make the association we want..Npm to the rescue again!.Are you ready for the magic?Not only does npm link create the global association we want between the say-hello command and the printHelloWorld.js executable file, but it also “updates” automatically when you change the contents of that file, because it is directly linked to the file itself via symbolic link..The last two lines of output before we use say-hello again show the associations being made.So, if we changed our source code toconsole.log('Goodbye World')and saved the file, then running say-hello would cause “Goodbye World” to print to the screen instead of “Hello World” without us having to do anything else, as long as the project directory and executable file are not moved..This will come in very handy later when we are doing more interesting things than printing “Hello World” to the terminal, and we want to test our program before publishing it.Note: Be aware that you must execute npm link from within the directory that contains the package.json file (which should be your project’s root directory) for it to work..To remove this link and disable the new command, simply execute npm unlink from that same directory.ConclusionIn this tutorial, we’ve learned how to create a brand new global shell command, and connect it to a Javascript program that executes in response to that command, using Node and npm..Of course, printing “Hello World” is pretty boring, so in part 1.1 we will start learning how to use Node’s built-in modules to do some more interesting and useful things in our script file(s).See ya next time folks, and remember: Always Be Coding!-J. More details

Leave a Reply