Herramientas
Tutorial: install Node.js and run your first script
Install Node.js and run JavaScript outside the browser, straight from the terminal.
Until now your JavaScript only lived inside the browser. Node.js lets you run JavaScript directly on your computer, without opening a single tab.
Step 1: install Node.js
Go to nodejs.org and download the "LTS" version (Long Term Support, the most stable one). Install it like any other program, clicking "Next" on everything.
Step 2: confirm it installed
node --version
npm --version
If you see two version numbers (something like v20.11.0), you've got Node and npm (its package manager) installed. If you get a "command not found" error, restart your terminal, it sometimes needs to "find out" something new got installed.
Step 3: write your first script
Create a file called greeting.js:
const name = 'World';
console.log(`Hello, ${name}, from outside the browser`);
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((total, n) => total + n, 0);
console.log(`The sum is: ${sum}`);
Step 4: run it
node greeting.js
You'll see the result directly in your terminal. No console.log trapped in the browser's dev tools: this runs on your computer like any other program.
Why does this matter?
Node.js is what powers tools like this very site under the hood (Next.js runs on Node), backend servers written in JavaScript, and basically the entire package ecosystem you'll install with npm install from now on.
What did you just learn?
- How to install Node.js and confirm it works.
- How to run a
.jsfile directly from the terminal. - That JavaScript is no longer stuck in the browser, and that opens a lot of doors.