Run a JavaScript file
Put code in a file and execute it
⏱ Est. ~6 min
01 · Read
The REPL is good for experiments, but real programs live in files. A JavaScript file is just a text file with the .js extension. You write code into a file, then tell Node to run it.
Every real app works this way — from tiny scripts to massive web apps. Code lives in files. You run the files.
02 · Real-machine exercise
Use the terminal to create a JavaScript file called hello.js. This command writes a single line of JavaScript into a new file.
echo 'console.log("Hello from a file!")' > hello.js03 · Real-machine exercise
Now run your file with Node. This tells Node to read hello.js and execute every line of JavaScript in it.
node hello.js
04 · Quiz
What command runs a file called app.js with Node?
- javascript app.js
- run app.js
- npm app.js
- node app.js
05 · Fill in the blank
The development loop is: write code, _____ it, check the output.
06 · Read
You just completed the write-run-test loop — the foundation of all software development:
1. Write code into a file 2. Run the file 3. See the result 4. Repeat
Every engineer, from a beginner to a senior at Google, does this loop thousands of times a day. The code gets more complex, but the loop stays the same.
Key points
- JavaScript files have the .js extension
- Run them with node filename.js
- The write-run-test loop is the heartbeat of development
07 · Quiz
You create a file called greet.js, run node greet.js, but nothing shows up in the terminal. What's the most likely reason?
- The file exists but doesn't have any console.log() or output statements
- Node.js can't read files with that name
- You should run npm greet.js instead of node greet.js
- JavaScript files need a .node extension to execute
Other lessons in this chapter
⚠ The full interactive experience needs JavaScript. Please enable it and reload.
※ This is an independent Traditional Chinese teaching project — not an official Anthropic product. Claude™ is a trademark of Anthropic, PBC.