We use the node command to run JavaScript files outside the browser. It lets us see the program's results on our computer.
Running scripts with node command in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
app.js file in the current folder.Node.js
node app.js
test.js inside the scripts folder.Node.js
node ./scripts/test.js
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!');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.
Practice
1. What does the
node command do when you run node script.js in your terminal?easy
Solution
Step 1: Understand the purpose of the
Thenodecommandnodecommand runs JavaScript files using the Node.js runtime environment.Step 2: Analyze the command
This command tells Node.js to execute the code inside the file namednode script.jsscript.js.Final Answer:
It runs the JavaScript code inside the filescript.jsusing Node.js. -> Option DQuick Check:
Runningnode file.jsexecutes the file [OK]
Hint: Node runs JavaScript files given as arguments [OK]
Common Mistakes:
- Thinking node opens files in editors
- Confusing node with a compiler
- Assuming node uploads files
2. Which of the following is the correct way to run a JavaScript file named
app.js using Node.js?easy
Solution
Step 1: Recall the correct syntax for running scripts with Node.js
The correct syntax isnode filename.jswithout extra words.Step 2: Check each option
node app.js matches the correct syntax exactly:node app.js.Final Answer:
node app.js -> Option BQuick Check:
Correct command syntax isnode filename.js[OK]
Hint: Use 'node' followed directly by the filename [OK]
Common Mistakes:
- Adding extra words like 'run' or 'execute'
- Swapping order of 'node' and filename
- Using commands not recognized by Node.js
3. Given the file
What will be the output when running
hello.js with this content:console.log('Hello, Node!');What will be the output when running
node hello.js?medium
Solution
Step 1: Understand what
Theconsole.logdoesconsole.logfunction prints the text inside the parentheses to the terminal.Step 2: Predict the output of running
Running the file executes the code and prints 'Hello, Node!' to the terminal.node hello.jsFinal Answer:
Hello, Node! -> Option AQuick Check:
console.logprints text to terminal [OK]
Hint: console.log prints text when run with node [OK]
Common Mistakes:
- Expecting code to print the code itself
- Thinking node throws syntax errors for valid code
- Assuming no output without browser
4. You try to run
node myscript.js but get an error: Error: Cannot find module './myscript.js'. What is the most likely cause?medium
Solution
Step 1: Understand the error message
The error says Node.js cannot find the filemyscript.jsin the current folder.Step 2: Identify the cause
This usually means the file is missing or the path is wrong, not that Node.js is missing or the code has syntax errors.Final Answer:
The filemyscript.jsdoes not exist in the current directory. -> Option AQuick Check:
File missing causes 'Cannot find module' error [OK]
Hint: Check file exists in current folder before running [OK]
Common Mistakes:
- Assuming Node.js is not installed from this error
- Adding extra words to the node command
- Confusing file not found with syntax errors
5. You have two files:
What will be the output when you run
index.js:
console.log('Start');
require('./helper.js');
console.log('End');
helper.js:
console.log('Helper loaded');What will be the output when you run
node index.js?hard
Solution
Step 1: Understand the order of execution in
The code first prints 'Start', then loads and runsindex.jshelper.js, then prints 'End'.Step 2: Trace the output step-by-step
First, 'Start' is printed. Thenhelper.jsruns and prints 'Helper loaded'. Finally, 'End' is printed.Final Answer:
Start Helper loaded End -> Option CQuick Check:
require runs the imported file immediately [OK]
Hint: require runs the file immediately in order [OK]
Common Mistakes:
- Assuming require runs after all code
- Mixing output order
- Expecting syntax errors without mistakes
