To run a JavaScript file with Node.js, open a terminal in the folder that contains the file and type node filename.js. Node.js reads the file, runs it from top to bottom and prints anything written with console.log to the terminal. This is the standard way to run a script outside the browser, and the search phrase is run javascript file node.
This guide assumes Node.js is installed. You will create a file, run it, pass arguments, read them from process.argv, and fix the most common errors.
Create a folder, open it in your editor and add a file named hello.js:
Save the file. process is a global object that Node.js provides, so you do not need to import it.
cd path/to/folder.The version will match your installation. The .js extension can be left out (node hello), but writing it is clearer. If the file is in another folder, give the path: node scripts/hello.js.
Everything after the file name is passed to your program. Read it from process.argv, an array where the first two items are the path of Node.js and the path of your script.
For real command-line tools, the built-in util.parseArgs function parses flags such as --name=Asha without installing a package.
While developing, Node.js can restart a script each time you save it:
It can also load variables from a .env file:
Both options exist in current LTS releases. Run node --help to see what your version supports.
Node.js can run two module formats. A file ending in .cjs uses require(), and a file ending in .mjs uses import. For a plain .js file, the nearest package.json decides: with "type": "module" it is an ES module, otherwise it is treated as CommonJS (current versions can also detect module syntax in some cases).
Using the node: prefix for built-in modules makes it clear that the import is not from npm.
| Message | Cause | Fix |
|---|---|---|
Cannot find module '/path/hello.js' | Wrong folder or file name | Check the path with ls or dir |
node: command not found | Node.js is not on your PATH | Reinstall or open a new terminal |
SyntaxError: Cannot use import statement outside a module | ES module syntax in a CommonJS file | Rename to .mjs or set "type": "module" |
ReferenceError: document is not defined | Browser-only object used in Node.js | Remove it or run in a browser |
Each run ends with an exit code. A script that finishes normally returns 0, and an uncaught error returns a non-zero number, which tools such as build scripts use to detect failure. You can set it yourself:
In a shell, check the last result with echo $? (macOS and Linux) or echo %errorlevel% (Windows Command Prompt). Writing errors with console.error sends them to standard error, so you can separate them from normal output when you redirect with node app.js > out.txt.
Press Ctrl+C in the terminal. A script that finishes its work ends by itself.
Yes, but give the full file name. Node.js treats a file without an extension as CommonJS unless configured otherwise.
You probably printed a variable that was never assigned or a function result that returns nothing. Check the value before the console.log call.
Recent versions can strip simple type annotations and run .ts files directly, but check the documentation for your version because the support has changed over time.
Save the code in a .js file and run it with node file.js from the right folder. Use process.argv for arguments, --watch while developing and the .mjs or "type": "module" setting for import syntax. Most errors come from the wrong folder, a missing install or the wrong module format.