Node.js and Command Line
Updated: Oct 5, 2022
Node.js is a runtime JavaScript environment that allows us to run JavaScript code outside a web browser. We can easily download and install Node.js in our system, about which we have already discussed in previous posts.
A program in Node.js is generally run through command line. There are multiple functionalities of command line which are important to know. Let us study them one by one.
Quick Links -
First of all, what is a command line?
COMMAND LINE
Command line is a computer environment or a sort of low-level interface for our computer, where instead of moving mouse and clicking to make things work, we are allowed to directly type commands for any functionality.
Let us now see, what works are done using command prompt in Node.js
TO RUN NODE.JS PROGRAM FROM COMMAND LINE
There is a command “node” that gets installed automatically along with the installation of Node.js in our system. A usual way to run Node.js program is to run the node command along with the name of the file we want to execute.
Suppose, the name of the file we want to run is “app.js”, the command we will be typing shall be –
node app.js
TO INSTALL MODULES
Let us take example of a module “nodemon”.
To install the module globally to system path, following command is to be run-
npm i –g nodemon
Where,
i = install, and
g = globally to system path
To install module as a development dependency, following command is to be run-
npm i –save-dev nodemon
The above command installs the module locally.
TO EXIT FROM A NODE.JS PROGRAM
There are multiple ways to terminate an application of Node.js.
The most comfortable and suitable way to terminate is to press ctrl + c. When a Node.js application is running in console, we can press ctrl + c to exit the application.
The other way is to use process module, which provides a handy method to programmatically exit a program: process.exit() .
Whenever node.js run this line, the program is immediately forced to stop. This means, any pending callback, or any other work which is still pending, will be ungracefully terminated.
The command goes like, with 1 being exit code -
process.exit(1);
There are many more uses of command line in Node.js. We will be discussing about them in upcoming posts.
Thank you.
Comments