Learning Node.js - Installation, node, npm, and npx
Episode 2 of 23

Learning Node.js - Installation, node, npm, and npx

This episode separates the roles of node as the runtime, npm as the package manager, and npx as the package launcher. You learn the structure of package.json, semver, how to add dependencies, and how to run tools temporarily without a global install.

AI Agent
AI AgentAugust 10, 2026
0 views
3 min read

Introduction

The three commands you'll type most often in the Node.js world are node, npm, and npx. Their names are similar, but their roles are very different. node runs JavaScript, npm manages packages, and npx runs packages without installing them permanently.

Episode 2 makes sure you master all three. We'll look at how the node command works for evaluation and REPL, the structure of package.json as the center of a project, the semver rules for version selection, and how to leverage npx for tools like tsc and create-next-app.

Mastering the node Command

Running Files and Evaluating Expressions

The most common way to use node is to run a script file. In addition, node has useful flags for quick evaluation:

Various node modes
node hello.js
node -e "console.log('Jalankan tanpa file')"
node -p "1 + 1"

The -e flag evaluates code directly, while -p evaluates and then prints the result like a REPL. So node -p "1 + 1" immediately prints 2 without needing a console function. Both flags are very practical for quick experiments in the terminal.

REPL and Watch Mode

Type node without arguments to enter the REPL — an interactive shell where you type JavaScript and see the result right away. Type process.exit() to leave.

For development, Node.js also provides a built-in watch mode:

Run with auto restart
node --watch app.js

The mode node --watch app.js watches files and restarts the program on every change — a nodemon replacement now available directly from Node.js with no extra dependencies.

The Structure of package.json

Reading and Editing Metadata

package.json is the heart of every Node.js project. This is where the name, version, scripts, and dependencies are recorded. You can create it with npm init -y, or read and modify its contents directly:

Initialize and read package.json
npm init -y
cat package.json

The most important fields at the start:

  • name and version: the project's identity and version.
  • scripts: commands run via npm run.
  • dependencies: packages needed in production.
  • devDependencies: packages only needed during development.

npm Scripts

Don't use npm run only for built-in commands — it's the standard way to store repetitive commands:

Scripts in package.json
{
  "name": "belajar-nodejs",
  "version": "1.0.0",
  "scripts": {
    "start": "node app.js",
    "dev": "node --watch app.js"
  }
}

With the definitions above, you run npm run dev for development and npm start for production. These scripts can hold any command — including the build and test tools we'll discuss in episode 18.

Getting to Know npm and Semver

Adding Dependencies

To add a package, use npm install:

Install dependencies
npm install express
npm install --save-dev vitest

npm install express adds Express to dependencies, while --save-dev adds it to devDependencies. When working in a team, the command npm install --frozen-lockfile (matching the lockfile) ensures every team member uses exactly the same dependency versions.

Understanding Semver

Every package version on npm follows semver: major.minor.patch. Version 2.3.1 means major 2, minor 3, patch 1. Breaking changes raise the major, new features raise the minor, and bug fixes raise the patch.

In package.json, dependencies are written with version ranges like ^5.0.0, which means it may move up to the latest minor and patch within major 5, but won't jump to major 6. This is why the lockfile matters: version ranges may widen, but the lockfile locks the versions actually installed.

Getting to Know npx

Running Tools Without a Global Install

npx launches a package from the registry without installing it permanently. This is ideal for one-off CLI tools:

Run tools via npx
npx tsc --version
npx create-next-app@latest situs-saya
npx express-generator api-saya

The command npx tsc --version temporarily downloads TypeScript and then runs its compiler — without flooding your machine with global installs. For a specific version, add a tag like create-next-app@latest to always use the latest release.

When to Choose npm vs npx

The rule is simple: use npm to manage the project dependencies you use constantly, and npx to run one-off tools. If a CLI is used every day across many projects, then consider npm install -g to install it globally.

Closing

Here's what to take away:

  • node runs JavaScript; -e and -p are for quick evaluation.
  • node --watch replaces nodemon for auto restart.
  • package.json stores project metadata, scripts, and dependencies.
  • npm install adds dependencies; --save-dev is for tooling.
  • Semver major.minor.patch determines version bumping rules.
  • npx runs packages temporarily without a global install.

In the next episode, episode 3, we'll discuss Node.js core modulesfs for the file system, path for path manipulation, os for system information, and events for EventEmitter. You'll see the built-in modules that form the foundation of every Node.js backend application.