Level: Advanced | Player: 24_Node_Environment
Learning Objectives
- Understand the role of
package.json. - Use
npm init,npm install, andnpm run. - Install and utilize external packages.
- Create a simple CLI with the
binfield.
1. package.json
The project's business card and configuration file. Contains dependencies, scripts, and meta information.
npm init -y
Minimum form:
{
"name": "my-app",
"version": "1.0.0",
"type": "module"
}
If you add "type": "module", .js will also operate as an ESM.
2. scripts
Register frequently used commands as aliases.
{
"scripts": {
"start": "node index.js",
"hello": "node -e \"console.log('hi')\""
}
}
Run:
npm start
npm run hello
start, test, etc. run without run.
3. dependencies
npm install chalk@5
Installed on node_modules/ and added to dependencies on package.json.
import chalk from 'chalk';
console.log(chalk.green('Success!'));
The development tool is --save-dev (or -D).
4. Creating CLI (bin)
{
"bin": { "greet": "./bin/greet.js" }
}
bin/greet.js first line shebang:
#!/usr/bin/env node
console.log('hi', process.argv[2] ?? 'world');
After npm link, greet Hong Gil-dong is possible in the terminal.
Core example
| folder | Contents covered |
|---|---|
| 01_init/ | minimal package.json |
| 02_scripts/ | npm scripts |
| 03_dependency/ | external package (chalk) |
| 04_cli/ | Creating CLI with bin field |
Run npm install from each folder.
⚠️ Common Mistakes
- Commit
node_modulesto git. - Ignoring
package-lock.json— version lock guarantee is broken. - Confusion over the meaning of
^,~(^5.0.0allows 5.x). - Missing
"type":"module"from ESM →importSyntaxError. - CLI shebang
#!/usr/bin/env nodeis omitted and cannot be executed directly.
❓ Frequently Asked Questions (FAQ)
Q1. yarn/pnpm? A. Alternative package manager. You can learn the basics with npm.
Q2. When to install globally? A. CLI tools. Project dependencies are installed locally.
Q3. How to clear dependencies? A. npm uninstall <package>.
🎉 Completed the course!
If you've completed all 25 parts, you can now try the following:
- Introduction to TypeScript — Adding a type system to JS
- Frameworks such as React/Vue
- Advanced Node.js (Express, database connection)
- Build tools (Vite, esbuild)
Thank you for your hard work!