How to Run a Script with Node (node app.js)
The live Node prompt is fun for quick tests, but it forgets everything the moment you close it. Type a few lines, hit enter, see the result—and then it’s gone. That’s fine for playing around, but it’s no way to build something real.
So how do you keep your code and use it again whenever you want? You save it in a file and run the whole thing with one command.
To run a script with Node, save your code in a file like app.js, then type node app.js in your terminal—Node reads the file top to bottom and shows you the output.
Why move from the live prompt to a file?
The live prompt (the Node REPL) is like talking out loud. You say something, you hear an answer, and then it’s forgotten. Close the window and your code is gone forever.
A file is different. Think of a recipe card. You write down every step once, you save the card, and you can cook from it any day you like. A code file works the same way: write it once, run it again and again.
That small shift—from typing into chat to saving a file—is what turns a fun experiment into real software. Real programs live in files, not in a chat window you’ll close in five minutes.
How do I run a script with Node?
First, create a file and put some code in it. Open your editor and save a file called app.js with one line inside:
console.log("Hello from my first script!");
Now open your terminal in the same folder and type:
node app.js
Press enter, and you’ll see your output appear:
Hello from my first script!
That’s it. Your whole file just ran in a single line.
What does “node app.js” actually mean?
The command has two simple parts, and it helps to name them.
nodeis the runner—the program that reads and executes your JavaScript.app.jsis your saved file—the code you want to run.
When you run a script with Node, it reads the file from top to bottom, line by line, and prints the output to your screen. The result of app.js is whatever your code says to show. No magic—just a runner and a file working together.
Why is a saved file better than a live prompt?
Because real code is saved, reused, and shared. A file gives you three things the live prompt can’t:
- It’s saved. Your code stays on disk, ready for next time.
- It’s reusable. Run it once or run it a hundred times—same file, same command.
- It’s shareable. Send
app.jsto a friend and they can run it too.
It’s a bit like submitting a real assignment file instead of just saying the answer out loud once and losing it. The file is the real deliverable.
Try it right now
Don’t just read—do it. This takes under a minute:
- Save a file as
app.jswith oneconsole.logline. - In your terminal, type
node app.jsand press enter. - Watch your whole file run and print the output.
If you saw your message on screen, congratulations—you just ran your first real script. This is the same move you’ll use for every Node program you ever build.
Key takeaways
- To run a script with Node, save your code in a file like
app.jsand runnode app.js. nodeis the runner;app.jsis your saved file; Node runs it top to bottom.- The live prompt forgets everything; a file saves your code so it’s reusable and shareable.
- A code file is like a recipe card—write the steps once, run them anytime.
- This one command is the foundation for every real Node program you’ll build next.
You’re learning to truly command your machine, one small step at a time. This lesson is part of the free Zero to AI Hero course, which takes you from complete beginner to building real apps with AI. If you haven’t yet, the previous lesson on how to talk to Node live in the REPL pairs perfectly with this one. Next up: what is npm, and how it lets you add other people’s code to your projects.
🚀 Take the full free course: Zero to AI Hero — learn to build with AI from scratch. New lessons daily, in Hindi & English.
Want to actually learn this?
151 free 2-minute lessons — from "what's a file?" to building with AI.
▶ Start the free course