25. Intro to npm
package.json is your project's business card and configuration file. It holds dependencies, scripts, and metadata.
What you'll learn
- 1Understand what `package.json` does.
- 2Use `npm init`, `npm install`, and `npm run`.
- 3Install and use external packages.
- 4Build a tiny CLI with the `bin` field.
Overview
package.json is your project's business card and configuration file. It holds dependencies, scripts, and metadata.
Core Concepts
1. package.json
package.json is your project's business card and configuration file. It holds dependencies, scripts, and metadata.
npm init -yMinimal form:
{
"name": "my-app",
"version": "1.0.0",
"type": "module"
}Adding `"type": "module"` makes `.js` files run as ESM too.
2. scripts
Register common commands as named aliases.
{
"scripts": {
"start": "node index.js",
"hello": "node -e \"console.log('hi')\""
}
}Run them:
npm start
npm run hello`start`, `test`, etc. don't need the `run` prefix.
3. dependencies
npm install chalk@5It installs into `node_modules/` and is added to `dependencies` in `package.json`.
import chalk from 'chalk';
console.log(chalk.green('success!'));Dev tools go under `--save-dev` (or `-D`).
4. Building a CLI (`bin`)
{
"bin": { "greet": "./bin/greet.js" }
}Add a shebang on the first line of `bin/greet.js`:
#!/usr/bin/env node
console.log('hi', process.argv[2] ?? 'world');After `npm link`, you can run `greet Hong Gildong` from any terminal.
Examples
| Folder | What it covers |
|---|---|
| 01_init/ | Minimal package.json |
| 02_scripts/ | npm scripts |
| 03_dependency/ | External packages (chalk) |
| 04_cli/ | Build a CLI with the bin field |
Run `npm install` in each folder before running it.
src/01_init/package.json
{
"name": "init-demo",
"version": "1.0.0",
"description": "Minimal npm init example",
"type": "module",
"license": "MIT"
}
src/02_scripts/index.js
/**
* The entry point that npm start invokes
*/
console.log('launched via the start script!');
console.log('current time:', new Date().toLocaleString('en-US'));
src/02_scripts/package.json
{
"name": "scripts-demo",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "node index.js",
"hello": "node -e \"console.log('Hello from npm script')\""
}
}
src/03_dependency/index.js
/**
* Using the chalk package
* First: npm install
* Run: npm start
*/
import chalk from 'chalk';
console.log(chalk.green('success message'));
console.log(chalk.red.bold('bold red text'));
console.log(chalk.blue('blue text'), chalk.yellow('yellow text'));
src/03_dependency/package.json
{
"name": "dependency-demo",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"chalk": "^5.3.0"
}
}
src/04_cli/bin/greet.js
#!/usr/bin/env node
/**
* A small CLI: greet Hong Gildong
* Try it: in this folder run npm link, then call `greet <name>`
*/
const name = process.argv[2] ?? 'world';
console.log(`Hello, ${name}!`);
src/04_cli/package.json
{
"name": "greet-cli",
"version": "1.0.0",
"type": "module",
"bin": {
"greet": "./bin/greet.js"
}
}
Common Mistakes
- Committing `node_modules` to git.
- Ignoring `package-lock.json` β you lose pinned versions.
- Confusing `^` and `~` (`^5.0.0` allows any 5.x).
- Forgetting `"type":"module"` in ESM and getting a SyntaxError on `import`.
- Missing the `#!/usr/bin/env node` shebang on a CLI, so it can't be run directly.
FAQ
Q1. What about yarn/pnpm?
Alternative package managers. Learn npm first. **Q2. When do I install globally?** A. For CLI tools only. Project dependencies should be local. **Q3. How do I remove a dependency?** A. `npm uninstall <package>`.
Practice
Build a CLI that prompts for a name and greets the user.
homework/README.md
## Homework 1 β Greeting CLI. Use `readline` to ask for a name, then print a greeting.
- Register a `start` script in `package.json`.
- Run with `npm start`.
- After input, print `Hello, NAME! Welcome.`
homework/answer/homework_01/index.js
/**
* Homework 1: prompt for a name with readline and greet
* Run: npm start
*/
import { createInterface } from 'node:readline/promises';
import { stdin as input, stdout as output } from 'node:process';
const rl = createInterface({ input, output });
const name = await rl.question('Enter your name: ');
console.log(`Hello, ${name.trim() || 'anonymous'}! Welcome.`);
rl.close();
homework/answer/homework_01/package.json
{
"name": "greet-input",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "node index.js"
}
}
Next Lecture
Congratulations β you finished the course!
Once you've worked through all 25 lectures, here's what to try next:
- Intro to TypeScript β adding a type system to JS
- Frameworks like React or Vue
- Deeper Node.js (Express, databases)
- Build tools (Vite, esbuild)
Nice work!
All lecture materials and example code are openly available on GitHub.
View on GitHub β