Node.js Get Started
To get started with Node.js, install it, check the version, and run your first files. This chapter also sets up a project folder that uses ES modules, which every later lesson relies on.
Install Node.js
Download the LTS installer from nodejs.org, or use a version manager such as nvm (macOS/Linux) or nvm-windows / fnm (Windows) so you can switch versions easily. Then confirm the install:
node -v
npm -vv20.11.1 10.2.4
The Node.js REPL
Type node with no arguments to open the REPL (Read-Eval-Print Loop). It is a quick playground for trying out JavaScript. Press Ctrl+C twice or type .exit to leave.
$ node
> 2 ** 10
1024
> [1, 2, 3].map(n => n * 2)
[ 2, 4, 6 ]
> .exit1024 [ 2, 4, 6 ]
Create a Project
npm init creates package.json, the file that describes your project. Add "type": "module" so .js files use import/export.
mkdir node-app && cd node-app
npm init -y
npm pkg set type=module{
"name": "node-app",
"version": "1.0.0",
"type": "module",
"main": "index.js",
"scripts": { "test": "echo \"Error: no test specified\" && exit 1" }
}Run a File
process.argv holds the command-line arguments. The first two items are the node binary and the script path, so we slice them off.
// index.js
const args = process.argv.slice(2);
console.log('Arguments:', args);
console.log('Current folder:', process.cwd());$ node index.js hello world Arguments: [ 'hello', 'world' ] Current folder: /home/naeem/node-app
Watch Mode and Scripts
Node 20 can restart your program automatically when files change, so you no longer need nodemon for simple projects. Add scripts to package.json and run them with npm run.
// package.json
"scripts": {
"start": "node index.js",
"dev": "node --watch index.js"
}
$ npm run devRestarting 'index.js' Arguments: []
Useful Tips
- Use a code editor with good JavaScript support, like VS Code.
- node --env-file=.env index.js loads environment variables (Node 20.6+).
- Top-level await works in ES modules — no async wrapper needed.
- Run node --help to see all runtime options.
Frequently asked questions
How do I run a JavaScript file in Node.js?
Open a terminal in the file's folder and run node filename.js. Node executes it and prints any console.log output.
What is the difference between Node LTS and Current?
LTS (long-term support) versions are stable and supported for about 30 months, ideal for production. Current versions get new features first but have shorter support.
Do I still need nodemon?
For most projects, no. Node 18.11+ has node --watch, which restarts the process when files change.