Writing a shell script from scratch in Node.js

Matthew Croak
May 20 · 5 min read
Photo by Campaign Creators on Unsplash

Shell scripts are very useful when working on a JavaScript app. If you’ve ever used create-react-app, that command essentially takes care of a lot of overhead needed when developing React apps.

Some of this overhead includes writing scripts you might use to execute or build your app, such as npm start or npm run build. These are great examples of commands that execute specific shell scripts.

After reading this, you will have successfully written a shell script from scratch in Node.js.

The script we are going to write is simple. Given the command npm run emoji, the script will run and console.log a random emoji in our terminal.

First things first, we need a file that will contain the code that the script will execute. Let’s call this file emoji.js. Once the file is created, we can start writing the script itself.

If you’ve ever looked at your package.json file (as a JavaScript developer it is imperative that you familiarize yourself with this file if you haven’t already), you’ve probably seen a section for scripts.

This is indicated by a key, written as the string “scripts”, pointing to an object value. This object contains the various scripts used in your project (like npm start).

Here’s an example of the script’s key-value pair in a package.json file after using create-react-app:

"scripts":{
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}

This object is where our new script will live. In your package.json file, write the following line in the scripts object:

"emoji": "node emoji.js" 

Now your scripts section should look something like this:

"scripts":{
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"emoji": "node emoji.js"
}

In the scripts object, the keys represent the command that will execute the values. So when we type the command npm run emoji, it will execute the command node emoji.js.

In Node, typing node + filename.js will run that JavaScript file in your command line. As mentioned earlier, the way we can run this script is by typing npm run emoji. But before we can run our script, let’s install our dependencies.

Write these lines in your command line to install the dependencies:

npm install node-emojinpm install random-emoji

We will be using two dependencies, node-emoji and random-emoji. Node-emoji is an npm package that provides emoji support for Node.js projects.

Node-emoji has a get function that accepts a string. This string is the shortcode (name) of the emoji. If you wanted to get the heart emoji, you’d write this:

var emoji = require('node-emoji');console.log(emoji.get('heart'));

Here’s the result in our terminal:

C:\project-directory> npm run emoji❤️

You could also write it this way:

var emoji = require('node-emoji');console.log(emoji.get(':heart:'));

This format is the traditional emoticon markdown that is supported by a number of online hosts, such as GitHub. This will come in handy for our next dependency, random-emoji.

Random-emoji returns a string in the traditional markdown format, including the beginning and end colons. The random-emoji function we will use to return this shortcode string is, simply enough, random.

If you want to return a random emoji shortcode, you’d write this:

var emoji = require('emoji-random');console.log(emoji.random());

And here’s what the return would look like:

C:\project-directory> npm run emoji:squirrel:

We can combine these dependencies and their associated functions to create our unique script that logs a random emoji, written as the emoji itself, not just the shortcode.

To do this, you need to call the random-emoji’s random function first. Once you get the random emoticon shortcode, you can pass that string to the node-emoji’s get function. This will retrieve the emoticon itself.

To see the final emoji in your terminal, just console.log the result. Here’s the code I wrote, with some formatting on the last line to indicate the emoji shortcode (minus the colons) as well as the emoticon itself.

var randEmoji = require('emoji-random'),
emoji = require('node-emoji');
var randString = randEmoji.random();var finalEmoji = emoji.get(randString);console.log('Here is the '+ randString.split(':')[1] + 'emoji: '+ finalEmoji);

And here is the logged result:

C:\project-directory> npm run emojiHere is the video_game emoji: 🎮

Now, while you can still run this script anytime you want by typing the command npm run emoji, we can take this further by calling this script every time we call other scripts.

Say we have a React app and we want to generate a random emoji in our terminal every time we try to start our app (more fun than functional), we can call our new emoji script within our start script.

Here is the line you need to call node emoji.js whenever you start your app:

"start": "react-scripts start & npm run emoji"

By combining our start and emoji script, we can execute the emoji script every time we run npm start. Without having to write npm run emoji or node emoji.js in our command line.

Your final scripts section in package.json should look something like this:

"scripts": {
"start": "react-scripts start & npm run emoji",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"emoji": "node emoji.js"
}

By including this in your package.json file, every time you start up your app you will also generate a random emoji. While this isn’t the most useful script, hopefully now you have a better understanding about scripts and how to implement your own.

If you enjoyed the post or need clarification on anything mentioned, let me know in the comments.


Better Programming

Advice for programmers.

Matthew Croak

Written by

UI Engineer at Analytic Partners || Dog Lover

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade