Automation Techs For Productivity And Fun

Tutorial To Create Command Line Programs With Node.js


As I mentioned in the post below, I recommend creating command line programs with Node.js when starting to learn Javascript.

https://blog.pyrospect.com/2019/01/why-javascript-is-best-programming.html

Most tutorials for Node.js starts with "How To Run A Web Application Server With Node.js", but if it's the first time for you to write a program, you should forget about Web for a while. Web Programming uses many kinds of technologies together, like HTML, CSS, HTTP, Client/Server... and it would surely make beginners confused.

So this is a tutorial about setting up Node.js and writing a simple command line program. It Node.js would be a help for you to write programs for automating tasks like converting image files, checking Excel files, and collecting data from Internet.

Setting Up Node.js Runtime Environment

Download installer from official site and install it to your PC.

https://nodejs.org/en/

After installing, "node" command and "npm" command should be added.

You can check it in command prompt by executing commands below:

node -v
npm -v

It would show you the version number of Node.js. if it's correctly installed.

"node" is the command to run Node.js programs.
If you want to run "sample.js" for example, execute a command as below:

node sample.js

"npm" (Node Package Manager) command is a command to download and install package libraries you use in Node.js programs.

You can find available packages here:

https://www.npmjs.com/

Creating A Node.js project

Let's create an example project named "HelloWorld" to check how to setup new Node.js projects.

First, open the command prompt and create a directory for your new project and move in it.

mkdir HelloWorld
cd HelloWorld

Next, execute the command below to initialize your Node.js project.

npm init

It asks you some questions about the project you are creating, but it's not important now, so just press Enter for each question.
This command creates a file named "package.json". It's a file that keeps information about your project.

Next, let's add a package to this project.

There are basic packages you can use without additional packages, but you would need to add some kind of packages for most projects.

This time, we would install "Moment.js": a popular package to control date and time.

https://momentjs.com/

You can install it by executing the command below:

npm install --save moment

This command downloads "Moment.js" with its depending packages to a directory named "node_modules" in your project directory.
"node" command references this directory when you call packages in your program.

The "--save" options saves the information of the package you installed to "package.json".

The name and version of the installed package would be written like this:

...
 "dependencies": {
  "moment": "^2.22.2"
 }
...

This setting is used when you run this project on other computers.
As mentioned above, additional packages are downloaded in "node_modules" directory with dependencies,
and it uses large file size.
So when moving or copying, or commiting a project to some where else, we usually exclude "node_modules" directory from the project and reinstall packages at where we use it.

You can reinstall all the packages written in "package.json" by the command below:

npm install

It's the same when you want to run a Node.js project created by other developers.
If there's a file "package.json" in the project, it means you can prepare the project by executing "npm install" command.

Finally, if you want to uninstall packages from your project, execute the command blow:

npm uninstall --save moment

This would delete "Moment.js" and its dependent packages from "node_modules" directory and "package.json".

You can find more information about "npm" command here in the official document:

https://docs.npmjs.com/packages-and-modules/getting-packages-from-the-registry

Write And Run A Node.js Program

At last your "HelloWorld" project is prepared, so let's write and run a Node.js program.

Create a file named "hello.js", with the content below:

console.log('Hello, World');

This is the most simple Node.js program.

Run the program by executing the command below in the command prompt:

node hello.js

It would show you a message "Hello, World".

This is all you have to do. Write programs with Javascript, and execute it with "node" command.

Next, let's make a program using "Moment.js" that we installed above.

Install the package again with npm command:

npm install moment

Create a file named "date_test.js" with this content:


// load Moment.js package
const moment = reqiure('moment');

const now = moment().format('MMMM Do YYYY, h:mm:ss a');

console.log(now);


And execute it:

node date_test.js

It would show you the current date and time.

This program is using the "moment" package that you installed with npm command. The "require" function loads the package with the name specified.
So now, you can find packages in "npm" site, install it with npm command, and use it in your Node.js program.

For more

Here's a official reference of Node.js basic packages:

https://nodejs.org/api/

For more information about Javascript programming basics, check out these documents. These are documents for using Javascript on Web, but most of the part would be useful for Node.js too.

https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics

https://javascript.info/

Conclusion

There aren't so many web sites explaining about Node.js programming for command line programs, but Node.js is really a good environment to create small programs for small tasks.

I hope this article help your start of programming with Node.js.


Share:

Search This Blog

Labels

Generate Data For Testing With PostgreSQL

When developing a software using PostgreSQL database as storage, the function "generated_series" is very useful to create data ...

Powered by Blogger.

Labels

Recent Posts