0
0
Node.jsframework~5 mins

Running scripts with node command in Node.js

Choose your learning style9 modes available
Introduction

We use the node command to run JavaScript files outside the browser. It lets us see the program's results on our computer.

You want to test a small JavaScript program quickly.
You need to run a server or backend code written in JavaScript.
You want to automate tasks using JavaScript scripts.
You are learning JavaScript and want to try code without a browser.
You want to run JavaScript tools or utilities on your computer.
Syntax
Node.js
node filename.js

Replace filename.js with your JavaScript file name.

Make sure you are in the folder where the file is saved or provide the full path.

Examples
This runs the app.js file in the current folder.
Node.js
node app.js
This runs test.js inside the scripts folder.
Node.js
node ./scripts/test.js
Starts Node.js interactive mode where you can type JavaScript directly.
Node.js
node
Sample Program

This simple program prints a greeting message to the console when run with node.

Node.js
console.log('Hello from Node!');
OutputSuccess
Important Notes

If you get an error like 'node: command not found', Node.js might not be installed or added to your system path.

You can stop a running Node.js program by pressing Ctrl + C in the terminal.

Summary

The node command runs JavaScript files on your computer.

Use it to test scripts, run servers, or try JavaScript without a browser.

Make sure your file path is correct and Node.js is installed.