Cover image for Using Node.js on your command line to quickly test JavaScript functions

Using Node.js on your command line to quickly test JavaScript functions

seanwelshbrown profile image Sean Welsh Brown ・3 min read

Node.js is a powerful, lightweight runtime environment that allows users to run JavaScript code outside of their browser.

Node has become so popular and ubiquitous that it's become something of an essential installation on the machine of any developer working and building web applications today.

While Node has hundreds of potential use cases, today we're going to focus on a very small and specific tip that can aid your JavaScript development: using Node on your command line to quickly run a local JavaScript file and test its output.

Very often when working on algorithms on LeetCode or when writing code for a project, you might find yourself wanting to quickly test a function (with your own test cases, or others) to make sure it's working properly before submitting it as a solution or moving forward with your work.

While you could write your own specific test files for your application (using something like Jest for instance), or test your functions in an online code editor, there may be circumstances where you'd like to test something quickly, easily and locally on your own machine with nothing more than a terminal window and a code editor. This is where Node comes in, short and sweet!

Step one in our process is to make sure you have Node installed on your machine if you don't already. Visit the official downloads page on the Node.js website to find steps to install the latest version of Node on your current operating system.

Now, for an example let's say for instance that you're working on an algorithm that involves removing the duplicate instances of a number in an array as one of its steps, and you want to make sure that your function is working properly before moving on. We can do that easily right from our command line!

We'll be working in MacOS' Terminal for this tutorial, but the process should be similar for other command line applications.

First off, open Terminal and create a file. It can be anything, but we'll use test.js:

Alt Text

Now, let's open that file up in our code editor! In this case we're using Visual Studio Code, so the command to open the file will be different for other editors:

Alt Text

Now, in our editor window we can paste or write in the function that we'd like to test. There are two key things here to note:

  1. We're creating a test case by creating a const named arr that will be passed in as our argument.
  2. We're calling the function as a console.log, a feature that normally prints something to the console in a browser; in this case our "browser" is our command line through Node!

Alt Text

Now we save the file and go back to our command line. Typing the following command will tell Node to open and run our test.js file:

node test.js

And we'll see the following:

Alt Text

And there we go!

We know that's the proper expected output (in this case an array with all duplicate numbers removed), and that it worked for the test case we gave. We could change the array to whatever we wanted, or we could console.log the function multiple times with different arguments in order to test further.

The key to what we're doing here is in that node command-- you can run any JavaScript file and see its output on the command line, giving you the ability to quickly test functions locally without relying on online editors or writing separate test files within your application. Awesome!

If you've come this far, thanks so much for reading! I hope this little tip helps out with your own development and adds one more tool to your toolbox.

Posted on by:

seanwelshbrown profile

Sean Welsh Brown


Full-Stack Web Developer; using a background in acting and voice-over to pursue a lifelong love of software and technology from a humanities perspective.


markdown guide

Nice tip. Another approach, if you're developing for the browser, you can also do this in the console. And (at least in Chrome Devtools) you can save Snippets under the Sources tab. When you run a snippet it automatically runs it through console.