Node.js — Run Node.js scripts from the command line (2024)

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:

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:

#!/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:

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

chmod u+x app.js

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

Pass string as argument to node instead of file path

To execute a string as argument you can use -e, --eval "script". Evaluate the following argument as JavaScript. The modules which are predefined in the REPL can also be used in script.

On Windows, using cmd.exe a single quote will not work correctly because it only recognizes double " for quoting. In Powershell or Git bash, both ' and " are usable.

node -e "console.log(123)"

Restart the application automatically

As of nodejs V16, there is a built-in option to automatically restart the application when a file changes. This is useful for development purposes.To use this feature, you need to pass the `--watch' flag to nodejs.

node --watch app.js

So when you change the file, the application will restart automatically.Read the --watch flag documentation.

Reading Time
2 min read
Authors
Contribute
Edit this page
Table of Contents
  1. Pass string as argument to node instead of file path
  2. Restart the application automatically
Node.js — Run Node.js scripts from the command line (2024)

FAQs

How to run node js script from command line? ›

Run Node.js scripts from the command line
  1. node app.js. Bash Copy to clipboard. ...
  2. #!/usr/bin/node. JavaScript Copy to clipboard. ...
  3. #!/usr/bin/env node // your javascript code. JavaScript Copy to clipboard. ...
  4. chmod u+x app.js. Bash Copy to clipboard. ...
  5. node -e "console.log(123)" Bash Copy to clipboard. ...
  6. node --watch app.js.

How to pass command line arguments in nodejs? ›

In Node. js, you can pass command line arguments to your script the same as you would for any other command line application. Simply type your arguments after the script path separated with a space as shown below: Copied!

How to run JavaScript in CMD? ›

Running JavaScript from a File

Step 2: Open your terminal or command prompt. Step 3: Use the cd command to navigate to the directory where your JavaScript file is located. Step 4: Once you're in the correct directory, run the script using the node command followed by your script's filename.

How to check node from command line? ›

To check your version of Node. js, open Terminal (or another CLI of your choice), and type the command node -v .

How to run script from command line? ›

Run a batch file
  1. From the start menu: START ➞ RUN c:\path_to_scripts\my_script.cmd, OK.
  2. "c:\path to scripts\my script.cmd"
  3. Open a new CMD prompt by choosing START ➞ RUN cmd, OK.
  4. From the command line, enter the name of the script and press return. C:\Batch> Demo.cmd. or.

What is the command to run a npm script? ›

To execute your Script, use the 'npm run <NAME-OF-YOUR-SCRIPT>' command.

How to pass command line arguments in npm run? ›

Sending a single argument to an npm script:

using NPM run hello, followed by –name=”Jane” in the terminal, we have passed a single argument to the script script. js. The script then uses this argument to output a greeting message “Hello Jane !”. const nname = process.

How to pass argument in command line? ›

Command-line arguments are handled by the main() function of a C program. To pass command-line arguments, we typically define main() with two arguments: the first argument is the number of command-line arguments and the second is a list of command-line arguments.

How to pass command line arguments to an executable? ›

In order to do this follow these steps:
  1. Right click Build Specifications in the Project Explorer of the completed project.
  2. Select new>>Application (EXE)
  3. Navigate to the Advanced category.
  4. Check the Pass all command line arguments to application box (Figure 3)
Oct 12, 2023

How to compile node.js code? ›

How to Compile Node. js Code
  1. Step 1: Download and Install Node. js Source Code. The first step to compiling Node. ...
  2. Step 2: Install Dependencies. Before you can compile Node. ...
  3. Step 3: Configure and Build Node. js. ...
  4. Step 4: Run the Make Command. After configuring Node. ...
  5. Step 5: Install Node. js.
Nov 20, 2023

How to run node server locally? ›

Running a Node. js Server Locally
  1. Step 1: Create a Simple Node. js Server. Initialize a Node. ...
  2. Step 2: Run the Server. Start the Server: In your terminal, run node server. js . ...
  3. Step 3: Stopping the Server. Stop the Server: To stop the server, simply press Ctrl + C in your terminal.
Feb 5, 2024

How to know if Node.js is running? ›

Using Command Prompt
  1. Open the Command Prompt.
  2. Type node -v and press Enter.
  3. If Node. js is installed, it will display the version number.

How to check if Node.js is installed command line? ›

To see if Node is installed, open the Windows Command Prompt, Powershell or a similar command line tool, and type node -v .

How to start a node js project in cmd? ›

Start a new Node.

js project we should run npm init to create a new package. json file for our project. Create a new empty directory in your development environment and run npm init . You'll then answer a few basic questions about your project, and npm will create a new package.

How to use command prompt in node js? ›

Opening Node Command Prompt

It is a computer environment the same as command prompt and an easy way to test simple Node. js/JavaScript code and allows to execute multiple javascript codes. we can simply run REPL on the command prompt using node command on the command prompt.

How to run using npm? ›

  1. npm run-script <command> [-- <args>] aliases: run, rum, urn. ...
  2. npm run test -- --grep="pattern" The arguments will only be passed to the script specified after npm run and not to any pre or post script. ...
  3. "scripts": {"test": "tap test/*.js"} instead of.
  4. "scripts": {"test": "node_modules/.bin/tap test/*.js"} ...
  5. . ...
  6. {

How do I run a script in Terminal bash? ›

To run a Bash script in Linux, you first need to make sure the script has executable permissions. You can do this by using the ` chmod +x` command followed by the script name. Once the script is executable, you can run it by typing ` ./` followed by the script name in the terminal.

Top Articles
United States - English
How to: Encrypt Your iPhone
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Jonah Leffler

Last Updated:

Views: 6674

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Jonah Leffler

Birthday: 1997-10-27

Address: 8987 Kieth Ports, Luettgenland, CT 54657-9808

Phone: +2611128251586

Job: Mining Supervisor

Hobby: Worldbuilding, Electronics, Amateur radio, Skiing, Cycling, Jogging, Taxidermy

Introduction: My name is Jonah Leffler, I am a determined, faithful, outstanding, inexpensive, cheerful, determined, smiling person who loves writing and wants to share my knowledge and understanding with you.