Lesson 51 of 55 · javascript
Node.js Fundamentals & npm
Duration: 15 mins
Node.js Fundamentals & npm
Node.js is a server‑side JavaScript runtime built on Chrome’s V8 engine. It lets you run JavaScript outside the browser and power APIs, CLI tools, servers, and more.
1️⃣ Install Node.js
- Windows / macOS / Linux: download the installer from https://nodejs.org or use a version manager such as nvm (
nvm install --lts). - Verify:
node -v # e.g. v20.8.0
npm -v # npm version
2️⃣ The REPL & core globals
$ node
> console.log('Hello from Node');
> process.argv // command‑line arguments array
> __dirname // directory of the current module
> __filename // absolute path of the current file
3️⃣ Modules – CommonJS (require/module.exports)
// utils.js
function add(a,b){ return a+b; }
module.exports = { add };
// app.js
const {add} = require('./utils');
console.log(add(2,3));
4️⃣ npm – the package manager
# initialise a project (creates package.json)
npm init -y
# install a library as a runtime dependency
npm install axios
# install a dev‑only tool
npm install --save-dev jest
package.json scripts let you run commands easily:
"scripts": { "start": "node app.js", "test": "jest" }
Run with npm start or npm test.
5️⃣ Environment variables
Create a .env file and load it with dotenv:
npm install dotenv
require('dotenv').config();
console.log(process.env.API_KEY);
Node gives you full access to the file system, networking, and many built‑in modules (fs, http, path, etc.).