Run Node.js scripts from the command line

TABLE OF CONTENTS

The usual way to run a Node.js program is to run the globally available node command (once you install Node.js) and pass the name of the file you want to execute.

If your main Node.js application file is app.js, you can call it by typing:

BASH
node app.js

Above, you are explicitly telling the shell to run your script with node. You can also embed this information into your JavaScript file with a "shebang" line. The "shebang" is the first line in the file, and tells the OS which interpreter to use for running the script. Below is the first line of JavaScript:

JS
#!/usr/bin/node

Above, we are explicitly giving the absolute path of interpreter. Not all operating systems have node in the bin folder, but all should have env. You can tell the OS to run env with node as parameter:

JS
#!/usr/bin/env node
// your code

To use a shebang, your file should have executable permission. You can give app.js the executable permission by running:

BASH
chmod u+x app.js

While running the command, make sure you are in the same directory which contains the app.js file.

Restart the application automatically

The node command has to be re-executed in bash whenever there is a change in the application. To restart the application automatically, use the nodemon module.

Install the nodemon module globally to system path:

BASH
npm i -g nodemon

You can also install nodemon as a development dependency:

BASH
npm i --save-dev nodemon

This local installation of nodemon can be run by calling it from within npm script such as npm start or using npx nodemon.

Run the application using the nodemon command followed by the application's file name:

BASH
nodemon app.js
    Contributors
  • flaviocopes
  •  MylesBorins
  •  fhemberger
  •  LaRuaNa
  •  ahmadawais
  •  akazyti