<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by michael sorensen on Medium]]></title>
        <description><![CDATA[Stories by michael sorensen on Medium]]></description>
        <link>https://medium.com/@voltx180?source=rss-69efee212864------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*QwWmwo2lsiCyOYdiZPfrJA.jpeg</url>
            <title>Stories by michael sorensen on Medium</title>
            <link>https://medium.com/@voltx180?source=rss-69efee212864------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 16 Apr 2026 05:19:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@voltx180/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[How to avoid NPM supply chain attacks.]]></title>
            <link>https://itnext.io/how-to-avoid-npm-supply-chain-attacks-2fb8d7eab4d3?source=rss-69efee212864------2</link>
            <guid isPermaLink="false">https://medium.com/p/2fb8d7eab4d3</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[node]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[supply-chain-risk]]></category>
            <category><![CDATA[npm]]></category>
            <dc:creator><![CDATA[michael sorensen]]></dc:creator>
            <pubDate>Sat, 19 Mar 2022 22:20:29 GMT</pubDate>
            <atom:updated>2022-03-21T17:10:30.054Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*eCwvs5mHzcHHb9Rm.jpg" /></figure><p>Okay, so you have heard the news; The NPM supply chain is completely vulnerable to the whims of a few devs and their egos, “Protestware” is all the rage and the whole javascript ecosystem a big stack of cards waiting to come tumbling down at any moment.</p><p>Alright now that we’re on the same page let’s take a deep breath, grab a cup of coffee and talk about how we can easily protect ourselves while continuing to do our work and quietly ignore all the world problems surrounding us. (kidding)</p><h4>Table of Contents</h4><ol><li>Why is this a big deal now?</li><li>How Supply Chain Attacks Work</li><li>Best Practices / How to use Docker without knowing Docker (it’s easy now I promise)</li></ol><h3>Why is this a big deal now?</h3><p>Honestly, these issues have been on the horizon for years. Developers who subscribe to other languages in other ecosystems will be quick to educate you on how bad javascript is because javascript developers will install just about anything to avoid writing code.</p><p>And well, as a javascript dev myself, I don’t think they’re wrong.</p><p>Recently and I mean, in the last 3 months we have had 2 high profile package authors publish malicious code to their code base and have potentially effected a large amount of end users.</p><p><a href="https://github.com/Marak">Marak</a>, the author of <a href="https://github.com/faker-js/faker">Faker.js</a> and <a href="https://github.com/Marak/colors.js">Color.js</a> published an update that infinitely pasted algo text in the terminal preventing any dependent application from running. Seemingly to protest his lack of compensation for contributing to larger projects.</p><p><a href="https://github.com/RIAEvangelist">Brandon Nozaki (Aka RIAEvangelist)</a>, the author of <a href="https://github.com/RIAEvangelist/node-ipc">node-ipc</a>, approved a <a href="https://github.com/RIAEvangelist/node-ipc/pull/270">pull request</a> into master that <a href="https://gist.github.com/ckcr4lyf/6d96c2bf42ec31c6362053ea275d80d5">replaces all file contents on the system with a heart emoji</a>. Effectively wiping everything. And while this “protestware” was targeting Russian and Belerusian computers,<a href="https://github.com/RIAEvangelist/node-ipc/issues/308"> an american NGO was impacted by this attack</a>.</p><p>This is not the first time malicious code has been published to NPM either. However, these have hit mainstream news sources because they are extremely popular and each get tens of millions of downloads every week. So how does this work and how can we protect ourselves?</p><h3>How Supply Chain Attacks Work (for context)</h3><p>Before going into the solutions it’s important that everyone reading this understands the problem. If you are already familiar with supply chain attacks I recommend skipping this portion.</p><p>A<em>nd before you google “Supply Chain Software Attack” on google bear in mind that the answers you will likely find there are a little different than what we’re talking about today since most of those examples reference organizations purchasing code from other organizations. Since NPM is open source it’s important to keep in mind that all the players are usually individuals.</em></p><p>I think analogies can be confusing so let’s get straight to the nitty gritty. NPM stands for “Node Package Manager”. It is a command line tool. It is a package registry and it is a company (that was recently purchased by github).</p><p>NPM the company provides the NPM command line tool for free and the Node.js community has elected to include it when you install node. Users can submit code (packages) to the NPM registry and users can install packages from the registry by using the CLI tool.</p><p>Since you are reading this you are probably doing this all the time already so what’s the problem?</p><p>Well I am getting to that. like I said you can skip this part if you want.</p><p>Writing code is challenging and/or tedious so it is very common for users to download a package from NPM to save them the hassle of re-writing the code themselves and wasting all that time and energy. As a result the new code now <strong>depends</strong> on the aforementioned package to function properly. Now if the new code is bundled into a package any code written on top of that will now depend on 2 packages. Here’s a diagram because that sounded confusing even to me.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/662/1*-hcidCogSiHstSSPOLGWPA.png" /><figcaption>node_modules light</figcaption></figure><p>So what’s the problem? And the answer is the reader’s lack of patience. Stay with me now.</p><p>If the user writing myHelloWorldApp.js installs world.js they might not even know they also installed hello.js unless they thoroughly reviewed the world.js github page which is unlikely.</p><p>And now if the author of hello.js publishes an “update” to hello.js to NPM. And that “update” over-writes the user’s hard drive with emojis well that would be a supply chain attack. <em>There we made it. Are you happy?</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/662/1*8M3qD3lG61QrbVCprcXqGg.png" /><figcaption>the author of hello.js updated their package to include malware. Now everything that depended on it is corrupted.</figcaption></figure><h4>In Summary</h4><p>A supply chain attack can occur anytime you are downloading or executing third party code on your machine. That’s why your OS is always asking you DO YOU TRUST THIS APPLICATION!?!? or some variation. All Open-Source Package Managers like NPM, PIP, etc are extremely vulnerable to these types attacks because anyone can upload anything to a trusted platform and it is up to the users to defend themselves. Luckily, the community does do a good job of policing itself and issues like these resolve themselves very quickly. Usually.</p><h3>Best Practices</h3><p>Okay, so now what?</p><p>Fortunately, there are a couple of relatively easy steps we can perform to not get burned like the aforementioned American NGO did when they installed node-ipc. I will list them all here and go into more detail below.</p><ol><li>trust no one not even your loved ones or the dog.</li><li>Trace every ip connecting to your computer and destroy the whole thing if you notice an unfamiliar ip address.</li><li>Take all the cash out of your bank and put it in your mattress</li><li>Stock up on re-fried beans and hope for the best.</li></ol><p>Okay, okay all jokes aside here’s the real list</p><ol><li>Pin your version numbers in package.json</li><li>Use a package-lock.json and install with npm ci</li><li>Use docker or a VM</li><li>Common sense</li><li>Use your own package servers</li><li>Use a vetting service</li></ol><p>To pin a version number in your package.json you just need to remove the ^ character that is automatically prepended to the version number when you install a package for the first time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/662/1*iJxJvCiXB7sJ_wq70VZsCg.png" /><figcaption>remove carrot to pin version number.</figcaption></figure><p>This will prevent NPM from installing the latest valid version and instead only download the version you know you can trust. For more information about the symbols in NPM packages go here: <a href="https://docs.npmjs.com/about-semantic-versioning">https://docs.npmjs.com/about-semantic-versioning</a> (I also encourage you to check out the version calculator)</p><p>But what about my dependency’s dependencies? Well that’s where package-lock.jsoncomes in. This file tracks every single package that is being used for your application and the version being used. If you npm i or npm install your packages and something updates, the package-lock.json file will also be updated to reflect that change.</p><p>We don’t want that though. We want package-lock.json to yell at us if we are trying to install a package that does not match the version that is listed.</p><p>That’s where npm ci or npm clean-install comes in. It will Error if it tries to install something that conflicts with the package-lock.json file. <em>For those of you who deal with </em><em>package-lock.json merge conflicts all the time, installing packages with this command will also help mitigate some of that.</em></p><h3>And Docker!</h3><p>I know, I know. For a lot of people docker has been this sort of beast that’s mostly lived in the DevOps world. YAML files are kind of gross and difficult to debug when you have a problem.</p><p>But, the Docker team has done an amazing job trying to make utilizing a VM easier and easier every year and now it’s so easy you can setup a persistent development environment in a container (<em>on windows no less!) </em>in less than 5 minutes.</p><p>Before, we go through that 10 second tutorial let’s talk about why it’s important.</p><p>Pinning your version numbers and reviewing code is fine and all and still important but, part of fun of developing is the ability to explore the internet and try stuff out without fear. Why do we need to tip-toe around because some developers decided they wanted to go on a power trip?</p><p>The answer is we don’t. We can set up a safe space to download anything we want while isolating it from the things we actually care about and Docker (and VSCode) make achieving that easy. Here are the steps to add docker to an existing project:</p><ol><li><a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers">Add the Remote Containers VSCode Extension</a></li><li>Navigate to your project directory</li><li>Press Ctrl+Shift+P and type “add development container configuration files”</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/1*ZlxgT9THRTYbDUK1-gJq6A.png" /></figure><p>4. Select Node.js (if it’s a node project of course)</p><p>5. Press Ctrl+Shift+P and type “open folder in container”</p><p>6. Download and install docker if necessary</p><p>7. Done! VSCode should have re-opened itself with a remote connection to your new container with all of your code in it.</p><p>Now you can install whatever you want without worrying about bricking your hard drive.</p><h4>Use your own package servers and vetting service</h4><p>This was a suggestion provided by a <a href="https://www.reddit.com/user/Laladelic">u/Laladelic</a> on Reddit. I am not very familiar with using either of these solutions but, the user did provide links to available solutions:</p><p>vetting service: <a href="https://snyk.io/">https://snyk.io/</a></p><p>package servers: <a href="https://jfrog.com/artifactory/">https://jfrog.com/artifactory/</a></p><h3>In Conclusion</h3><p>None of these solutions are foolproof. Misconfiguration or misunderstanding of the above solutions can defeat their purpose.</p><p>It is also up to all of us individually to decided what the right level of security is. How much risk we are willing to take.</p><p>There is no such thing as perfect security but, I make these suggestions because I love open source and would hate to see its reputation tarnished because a false sense of trust was attributed to strangers on the internet.</p><p>Be safe out there.</p><p>P.S. If you want to correct or add anything to what I have written please write a comment and I will do my best to update this post as fast as I can!</p><p>[Edit] updated to include package server and vetting service suggestions</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2fb8d7eab4d3" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/how-to-avoid-npm-supply-chain-attacks-2fb8d7eab4d3">How to avoid NPM supply chain attacks.</a> was originally published in <a href="https://itnext.io">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A beginner’s guide to Electron.js]]></title>
            <link>https://medium.com/@voltx180/a-beginners-guide-to-electron-js-1679fd7b6e4f?source=rss-69efee212864------2</link>
            <guid isPermaLink="false">https://medium.com/p/1679fd7b6e4f</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[electron]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[michael sorensen]]></dc:creator>
            <pubDate>Wed, 12 Jun 2019 17:28:51 GMT</pubDate>
            <atom:updated>2020-07-24T17:44:16.424Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Objective:</strong> This guide will teach you how to develop and distribute an Electron.js application.<br><strong>Audience:</strong> This guide is targeted at Electron beginners. It is <strong>strongly</strong> recommended that readers have at least fundamental knowledge of HTML/CSS and Node.js as the Electron framework is built around these items.</p><h4><strong>Part 0:</strong> Understanding Electron</h4><p>If you found this guide by wanting to develop cross platform applications then you probably know that Electron.js does just that. You can easily develop and distribute applications for windows/macOS/linux with the same code (bear in mind this does not include android <em>or</em> iOS).</p><p>The question becomes, “How does Electron accomplish this?”. In short; Electron launches a headless <a href="https://www.chromium.org/Home">chromium</a> browser which has access to the <a href="https://nodejs.org/docs/latest/api/">Node.js API</a> via Electron’s own API. Which has plenty of use cases but, probably the biggest being is that your app can theoretically work without an internet connection.</p><p>If that sounded like a bunch of wumbo that’s okay but, it’s important to understand the Electron combines the browser and Node.js to create this seamless development experience for us.</p><h4>Part 1: Your first Electron App (AKA Hello World — again)</h4><p><em>Inspired by the getting started page </em><a href="https://electronjs.org/docs/tutorial/first-app"><em>https://electronjs.org/docs/tutorial/first-app</em></a><em> and assuming you have performed step 0 of </em><a href="https://medium.com/@voltx180/a-beginners-guide-for-creating-command-line-programs-in-node-js-42d1ebfe9c08"><em>my previous guide</em></a><em>.</em></p><p><strong>Step 0. </strong>Navigate to your project folder (you can just create a new folder wherever on your computer) and run the command npm init and follow the prompt provided</p><p><strong>Step 1.</strong> NPM Install Electron by running npm install electron --save-dev *NOTE* we use --save-dev instead of --save so you can work on multiple apps with multiple electron version the future.</p><p><strong>Step 2.</strong> Create two new files called index.js and index.html</p><p><strong>Step 3. </strong>Inside index.js enter the following code:</p><pre>const { app, BrowserWindow } = require(&quot;electron&quot;);<br></pre><pre>function createWindow() {</pre><pre><em>    // Create the browser window.</em></pre><pre>    let win = new BrowserWindow({</pre><pre>        width: 800,</pre><pre>        height: 600,</pre><pre>        webPreferences: {</pre><pre>            nodeIntegration: true</pre><pre>        }</pre><pre>    });</pre><pre><em>    // and load the index.html of the app.</em></pre><pre>    win.loadFile(&quot;index.html&quot;);</pre><pre>}</pre><pre>app.on(&quot;ready&quot;, createWindow);</pre><p>In this file we are importing the app and BrowserWindow modules from the Electron package we installed earlier. When the Electron app is ready we create a new BrowserWindow with the provided properties and load in our index.html file.</p><p><strong>Step 4.</strong> Put some content into your index.html file. <br>It can be whatever you want but, I like to use &lt;h1&gt;Hello World&lt;/h1&gt; for old time’s sake.</p><p><strong>Step 5.</strong> Running your application<br>There are a couple of gotchya’s to running an electron application. The first of which is that you might expect to run your app via node index.js just as you would with any node application.</p><p>Unfortunately, that won’t work. You need to run your javascript via Electron’s binary which is conveniently included in your Node_Modules folder. If you have looked at Electron’s documentation you might think you can run your app via electron . . Which is close but, won’t work unless you do some additional setup configuring your environment variables and having a global Electron installation. Which is beyond the scope of this guide.</p><p>Instead we will make an npm script by opening the package.json file (which was created when we executed npm init earlier) and adding a new start script with the property electron . . Your package.json should look something like the one below:</p><pre>{</pre><pre>    &quot;name&quot;: &quot;electron-part1&quot;,</pre><pre>    &quot;version&quot;: &quot;1.0.0&quot;,</pre><pre>    &quot;description&quot;: &quot;An electron tutorial&quot;,</pre><pre>    &quot;main&quot;: &quot;index.js&quot;,</pre><pre>    &quot;scripts&quot;: {</pre><pre>        &quot;start&quot;: &quot;electron .&quot;,</pre><pre>        &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot; </pre><pre>    },</pre><pre>    &quot;author&quot;: &quot;&quot;,</pre><pre>    &quot;license&quot;: &quot;ISC&quot;,</pre><pre>    &quot;devDependencies&quot;: {</pre><pre>        &quot;electron&quot;: &quot;^5.0.3&quot;</pre><pre>    }</pre><pre>}</pre><p>Finally, you can run your application by entering npm start in the terminal. And you should see a window that looks like this if you’re on a mac:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*wAzB0ij6TG55cxAVeeicwA.png" /><figcaption>First Electron application</figcaption></figure><p><strong>Part 2: Your actual first Electron app.</strong></p><p>Okay hello world is great and all but, let’s try and practice with some of the actual functionality Electron provides. Don’t throw away the Hello World app because we will just be building on it.</p><p><strong>Step 0.</strong> Add some styling<br>One of Electron’s biggest draws is that you can create an amazing custom GUI as easily as you can write html/css which is a hell of a lot easier than Qt and other native apps.</p><p>Create a css file called styles.css and include it in the root of your project and add the proper reference in your index.html file:</p><pre>&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;./styles.css&quot; /&gt;</pre><p>Any changes you make to your styles will show up when you re-run your electron project npm start .</p><p>I personally like to use <a href="http://getskeleton.com/">skeleton.css</a> for these demos because it’s lightweight and we won’t be needing much (and I’m too lazy to write css sometimes). <em>It is important to remember that if you want your app to look the same offline you can’t include the cdn script in your html. You need to download the css file and reference it locally.</em></p><p>This guide assumes you know how to write html and css so I won’t go over that and you can just steal it from the github that I have linked at the bottom. In any case my electron app now looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*xwB-Jnht8W4FbujImXmMWw.png" /><figcaption>Add a button that does something</figcaption></figure><p><strong>Step 1. </strong>Add some functionality</p><p>Before you add the jQuery to your html file (which you totally can) let’s focus on adding functionality that does not occur in your normal browser. Our goal here is to be creating a text file and saving it to our computer.</p><p>*Note* You may have downloaded files to your computer before but, those are likely from server requests. Javascript running in the browser does not have write or read permissions to your file system.</p><p><strong>1.1</strong> Create a new file called app.js with the following code. This our javascript file that will only be running on the browser to capture user input.</p><pre><em>// note that the fs package does not exist on a normal browser</em></pre><pre>const fs = require(&quot;fs&quot;);</pre><pre><em>//a dialog box module from electron</em></pre><pre>const { dialog } = require(&quot;electron&quot;).remote;</pre><pre><em>// Also note that document does not exist in a normal node environment</em></pre><pre><em>// button click event</em></pre><pre>document.getElementById(&quot;mybutton&quot;).addEventListener(&quot;click&quot;, () =&gt; {</pre><pre>    const data = &quot;Successfully wrote to the desktop&quot;; <em>// the data we want to save to the desktop</em></pre><pre><em>    //launch save dialog window</em></pre><pre>    dialog.showSaveDialog(filename =&gt; {</pre><pre><em>        //save file at the destination indicated by filename</em></pre><pre>        fs.writeFileSync(filename + &quot;.txt&quot;, data, &quot;utf-8&quot;, () =&gt; {</pre><pre>            console.log(&quot;attempted to write to the desktop&quot;);</pre><pre>        });</pre><pre>    });</pre><pre>});</pre><p><strong>1.2 </strong>If you have added a button with the id “mybutton” then the next time you run your node application and click that button, you should see a success.txt automatically be generated where you decided to save it.</p><p>Not too bad right? More functionality will come the more you learn about Node and Javascript in general. But, hopefully now you have a solid fundamental understanding of how Electron works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*Hg4rzhXFaTZ3y_ulRWl4bw.png" /><figcaption>Final outcome</figcaption></figure><p><strong>Step 2. </strong>Packaging your app</p><p>You probably know by now you can’t just hand your potential users the project files and hope they figure out how to run everything on their own. They likely don’t have node installed and why should they?</p><p>What we need to do is compile our application into a binary that our end users can simply double click and execute. To do that, install the electron packager by typing the following in your terminal:</p><pre>npm install electron-packager --save-dev</pre><p>Then add the following line to your package.json scripts</p><pre>&quot;package&quot;: &quot;electron-packager .&quot;,</pre><p>Finally execute the packager by typing the following in your terminal:</p><pre>npm run package</pre><p>You should see a new folder in your project directory that matches your current system. Included in that folder is an application (or .exe if you are using windows).</p><p><strong>In Conclusion</strong>: This guide should have given you a fundamental understanding of how Electron works. If you had trouble following this guide I suggest spending more time learning Node.js before jumping into Electron. If this guide was too simple, I highly suggest checking out the following resources:</p><ul><li>Electron Documentation: <a href="https://electronjs.org/docs">https://electronjs.org/docs</a></li><li>Electron Packager Documentation: <a href="https://github.com/electron-userland/electron-packager">https://github.com/electron-userland/electron-packager</a></li></ul><p>GITHUB: <a href="https://github.com/Mikkal24/tutorials/tree/master/electron-part1">https://github.com/Mikkal24/tutorials/tree/master/electron-part1</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1679fd7b6e4f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A beginner’s guide for creating Command Line programs in Node.js]]></title>
            <link>https://medium.com/@voltx180/a-beginners-guide-for-creating-command-line-programs-in-node-js-42d1ebfe9c08?source=rss-69efee212864------2</link>
            <guid isPermaLink="false">https://medium.com/p/42d1ebfe9c08</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[beginner-javascript]]></category>
            <category><![CDATA[beginner-coding]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[michael sorensen]]></dc:creator>
            <pubDate>Wed, 29 May 2019 00:11:32 GMT</pubDate>
            <atom:updated>2019-05-29T16:26:38.738Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xUMd403zFGXVckHqx-tRdw.png" /></figure><p><strong>Objective:</strong> This guide will teach you how to write a program that accepts command line arguments using Node.js.<br><strong>Audience: </strong>People who have never written a single Node.js program but, are somewhat familiar with the terminal and have some basic programming knowledge.</p><p><strong>Step 0:</strong> Setup<br>Most guides like these already have environment setup instructions so I won’t dive too deep here but, for the uninitiated here is a summary of the steps you should take and a few relevant links:<br>1. Install <a href="https://nodejs.org/en/">node.js</a></p><p>2. Ready a terminal (powershell, gitbash, mac terminal, etc)</p><p>3. Verify that node.js is installed by opening a terminal and typing node -v in the terminal(this guide was written using v10.13.0)</p><p>4. Ready a favorite text editor (I prefer <a href="https://code.visualstudio.com/">Visual Studio Code</a>)</p><p><strong>Step 1</strong>: Console Logging and Hello World<br>Yep, it’s another hello world application. You already know what to expect so let’s get right into it.</p><ol><li>Create a new file named hello-world.js (for those terminal enthusiasts just type touch hello-world.js)</li><li>Open that file with your favorite text editor and type console.log(&quot;hello world!&quot;); on the first line. To those new to javascript, console.log prints whatever string you pass as an argument similar to python’s print function. Go ahead and save that file.</li><li>Open or go back to your terminal and change your directory (cd) to where you saved that javascript file we just created. Inside the terminal type node hello-world (note that the .js file ending is not strictly necessary). You should see the output hello world! in your terminal. Congratulations you just created and executed your first Node.js program.</li></ol><p><strong>Step 2:</strong> Process.argv and accepting user input<br>Alright so we know how to run a node.js program but logging to the console is not terribly useful. We do know that most command line tools accept some form of input (ex. cd &lt;some location&gt;). How do we do that in Node.js?</p><p>Node.js exposes a global variable called <a href="https://nodejs.org/api/process.html#process_process">Process</a> that we can use to grab information from the user. Now let’s see exactly how we can do that.</p><ol><li>Create a new file called hello.js</li><li>Open that file and insert the following snippet and save the file.</li></ol><pre>const name = process.argv[2];</pre><pre>console.log(&quot;hello &quot; + name);</pre><p>In short, we are storing a user submitted value into a constant variable we call name then we are logging it into the console. There are a couple things which I encourage you to go mull over like the const keyword and string concatenation but, those are beyond the scope of this guide.</p><p>3. Run the program by typing node hello samantha wherever you saved the file. You should find that the program responds with hello samantha . You should then repeat this using a different name to confirm that it is capturing user input. Neat!</p><p><strong>Step 3. </strong>Process.argv continued…<br>Okay there’s a lot to unpack with process.argv but, in an attempt to sum it up it is an array of values corresponding to the values entered in the command.</p><p>For example; the last program we ran was node hello samantha if you console.log(process.argv) you should see values like this:</p><pre>[ &#39;/your/nodejs/installation/path&#39;, &#39;/the/path/to/your/hello.js&#39;, &#39;samantha&#39;]</pre><p>You can include virtually as many command line arguments as you need thanks to this feature.</p><p><strong>Step 4. </strong>adding functionality<br>To finish off this guide I want to leave you with the ability to read more in-depth node.js guides from any time period. With that let’s write a program that manipulates some of the values the user provides.</p><ol><li>Create a new file called add.js</li><li>Open that file in your text editor and add code that accepts 2 values and stores those values into variables.</li></ol><pre>const num1 = process.argv[2];</pre><pre>const num2 = process.argv[3];</pre><p>3. Include a function that adds those values together</p><pre>function add(param_num1, param_num2) {</pre><pre>    return param_num1 + param_num2;</pre><pre>}</pre><p>4. Call that function and store the result in a variable called sum</p><pre>const sum = add(num1, num2);</pre><p>5. Log sum to the console</p><pre>console.log(`sum: ${sum}`);</pre><p>Now if you run the program by typing the following into your terminal: node add 2 2 you might be surprised to see the number 22 appear on your screen. That is because Node.js can only interpret user input as a string data type. Data types in javascript can be a little tricky if coming from a more strictly typed language so I highly suggest reviewing <a href="https://www.w3schools.com/js/js_datatypes.asp">W3Schools article on the subject</a>.</p><p>To resolve this issue we can change parse the values to be numbers like</p><pre>const num1 = parseInt(process.argv[2]);</pre><pre>const num2 = parseInt(process.argv[3]);</pre><p>So if we run the program we should see the expected output of 4 .</p><p><strong>**A note on functions**<br></strong>Functions can take many different shapes in javascript and there are many ways to declare them. For experienced developers the differences are important but, for beginners the end result is mostly the same.</p><ul><li>function(){ //some logic } is an anonymous function</li><li>function myFunction(){ //some logic } is a named function</li><li>const myFunction = function(){ // some logic } is also a named function</li><li>const myArrowFunction = () =&gt; { // some logic } is a named <em>arrow</em> function</li></ul><p>You may see functions appear in different forms on your journey to learn Node.js and javascript. Javascript has been changing since its inception and will likely continue to while old guides will still be out there on the internet. Hopefully this information will shed some additional insight into what otherwise might be a confusing read.</p><p><strong>Github Repository: </strong><a href="https://github.com/Mikkal24/tutorials/tree/master/cli-part1">https://github.com/Mikkal24/tutorials/tree/master/cli-part1</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42d1ebfe9c08" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>