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
npmandnpx
💡 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
npm init -y
{
"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
npm install express
{
"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.
npx --version
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 existingpackage.json, the standard first step after cloning a project.npm install <package>, installs a new package and adds it todependencies.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 frompackage.json’sscriptssection.
Try It
- Run
npm init -yin a new empty folder, and inspect the generatedpackage.json. - Install any small package (like
express) and confirm it appears underdependencies. - Run
npx --versionand confirm it prints a version number without any separate installation step. - Explain, in your own words, the difference between
npm install <package>andnpx <package>.
Recap
package.jsonis a project’s manifest,npm initcreates one.npm install <package>downloads a dependency and records it inpackage.jsonandpackage-lock.json.npminstalls and manages packages,npxruns a package’s command-line tool, often without installing it permanently.
Next lesson: Node’s two module systems, CommonJS and ES Modules.