CodingNic

Node.js Fundamentals

package.json, npm, and npx

Node.js Fundamentals 12 min read

package.json, npm, and npx

Objectives

By the end of this lesson, you should be able to:

  • Create a new project with npm init
  • Install and understand a package’s entry in package.json
  • Explain the difference between npm and npx

💡 Why this matters: Every real Node.js project, including Express itself, starting in Module 5, is installed and managed through npm. This is the tooling layer every remaining module in this course depends on.

⚠️ A note on verification: every command and output in this lesson was actually run.

Creating a Project with npm init

bash
npm init -y
json
{
  "name": "node103",
  "version": "1.0.0",
  "description": "",
  "main": "eventloop.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

npm init creates a package.json file, the manifest describing a Node.js project, its name, version, dependencies, and scripts. The -y flag accepts every default value without asking, useful for quickly starting a new project (leaving off -y prompts for each field interactively instead).

Installing a Package

bash
npm install express
json
{
  "name": "node103",
  "version": "1.0.0",
  "dependencies": {
    "express": "^5.2.1"
  }
}

npm install <package> downloads a package (from the public npm registry) into a node_modules folder, and adds it to package.json’s dependencies, recording exactly what the project depends on. The ^ before the version number means “compatible with this version or newer, within the same major version,” allowing safe minor updates without accidentally jumping to a version with breaking changes.

npm install also creates a package-lock.json file, recording the exact resolved version of every dependency (including dependencies of dependencies), so a project installs identically on any machine.

npm vs npx

npm manages packages, installing them into a project. npx runs a package’s command-line tool, without necessarily installing it permanently. This distinction matters for tools you want to run once (like a project generator) without adding them as a permanent project dependency.

bash
npx --version
text
10.9.8

npx ships bundled with npm itself, no separate installation needed. A common real use: npx create-react-app my-app downloads and runs the React project generator just once, without leaving it installed afterward.

Common npm Commands

  • npm install (no package name), installs every dependency already listed in an existing package.json, the standard first step after cloning a project.
  • npm install <package>, installs a new package and adds it to dependencies.
  • npm install --save-dev <package>, installs a package as a development dependency (like a testing tool), needed only while developing, not in production.
  • npm run <script>, runs a named script from package.json’s scripts section.

Try It

  1. Run npm init -y in a new empty folder, and inspect the generated package.json.
  2. Install any small package (like express) and confirm it appears under dependencies.
  3. Run npx --version and confirm it prints a version number without any separate installation step.
  4. Explain, in your own words, the difference between npm install <package> and npx <package>.

Recap

  • package.json is a project’s manifest, npm init creates one.
  • npm install <package> downloads a dependency and records it in package.json and package-lock.json.
  • npm installs and manages packages, npx runs a package’s command-line tool, often without installing it permanently.

Next lesson: Node’s two module systems, CommonJS and ES Modules.