What Node.js does and what you need
JavaScript can run in a web browser, but it can also run directly on your computer with Node.js. That makes Node.js useful for small scripts, command-line tools, and learning programming basics without first building a web page. In this guide, you will write one file, ask Node.js to run it, and read the result in a terminal.
You need a computer with permission to install software, a plain-text editor, a terminal, and Node.js. Install the LTS version offered by the official Node.js download page for your operating system, following its installer instructions. LTS stands for long-term support; the exact version number may change over time. The examples here use basic JavaScript and do not require extra packages or a particular editor.
Check your Node.js installation
Open a terminal. On Windows, you can use PowerShell or Command Prompt. On macOS or Linux, use a terminal application. Type node --version and press Enter. If Node.js is available, the terminal prints a version number beginning with v. You can also type npm --version to check that npm, the package manager commonly installed with Node.js, is available, although you will not need npm for this program.
If the terminal says it cannot find node or does not recognize the command, finish the installation and open a new terminal window before trying again. A newly opened terminal may be needed to pick up changes made by the installer. If the command still fails, check that you installed Node.js for your operating system and that its installation directory is on your system’s PATH. You do not need to solve npm problems before continuing if node --version works.
Create and run a JavaScript file
Make a folder for your practice files, such as first-node-program, and open it in your text editor. Create a plain-text file named hello.js inside that folder. Make sure the name ends in .js rather than .js.txt; some systems hide known file extensions. Put this single line in the file: console.log("Hello, world!"); Save the file. The console.log function sends text to the terminal, and the quotation marks identify the text you want to print.
In your terminal, move into the folder using cd followed by its path. For example, if you created first-node-program inside your current directory, type cd first-node-program. If a folder path contains spaces, surround the path with quotation marks. Then type node hello.js and press Enter. You should see Hello, world! on its own line, followed by a new terminal prompt. Node.js reads the file, runs its instructions, and exits because there is nothing else for this program to do.
After changing the text in hello.js, save it and run node hello.js again. Saving matters: Node.js reads the file on disk, not unsaved changes in your editor. You can also run a file by giving node its full path, but working inside one practice folder keeps the command short and makes it easier to tell which file you are running.
Make the program respond to an argument
Next, create a second file named greet.js in the same folder. Put these two lines in it: const name = process.argv[2] || "friend"; and console.log("Hello, " + name + "!"); Put each statement on its own line, then save the file. The const statement gives the variable name a value. The plus signs join pieces of text into one greeting.
Run node greet.js Sam. The output should be Hello, Sam! Run node greet.js with no name, and the output should be Hello, friend! The terminal passes words after the filename to the program as arguments. Node.js stores them in process.argv; position 2 is the first word you supplied after the filename. The || "friend" part provides a fallback when that argument is missing. For now, try a single-word name so you can see that behavior clearly.
Fix common first-run errors
If you see an error saying Node.js cannot find a module or file, check the filename and your terminal’s current folder. On most terminals, pwd shows the current folder; in Command Prompt, cd by itself does the same. You can also list files with ls in many terminals or dir in Command Prompt. Confirm that hello.js or greet.js is there, then run the command again. Capital letters can matter on some operating systems.
If you see a SyntaxError, compare your file with the example. Check for missing quotation marks, parentheses, or other characters, and make sure you typed JavaScript into the file rather than a terminal command. If you typed node alone and now see a > prompt, you entered Node.js’s interactive mode instead of running a file. Type .exit to return to your terminal, then run node hello.js. These checks form a useful debugging habit: read the error, identify the file or line it points to, change one thing, save, and try again.



