JavaScript + Node.js track

Lesson 25 · Part 7: Node.js Fundamentals

Introduction to npm

`package.json`, scripts, dependencies and building a CLI

Part 7: Node.js Fundamentals2 min readFree lesson

Level: Advanced | Player: 24_Node_Environment

Learning Objectives

  • Understand the role of package.json.
  • Use npm init, npm install, and npm run.
  • Install and utilize external packages.
  • Create a simple CLI with the bin field.

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

  1. Commit node_modules to git.
  2. Ignoring package-lock.json — version lock guarantee is broken.
  3. Confusion over the meaning of ^, ~ (^5.0.0 allows 5.x).
  4. Missing "type":"module" from ESM → import SyntaxError.
  5. CLI shebang #!/usr/bin/env node is 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!

Example files

The runnable files used in this lesson. Copy them into your own project and experiment.

src/01_init/package.jsonJSON
{
  "name": "init-demo",
  "version": "1.0.0",
  "description": "npm init minimal example",
  "type": "module",
  "license": "MIT"
}
src/02_scripts/index.jsJavaScript
/**
 *Entry point run with npm start
 */
console.log('Runs as a start script!');
console.log('Current time:', new Date().toLocaleString('ko-KR'));
src/02_scripts/package.jsonJSON
{
  "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.jsJavaScript
/**
 *Example of 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('red bold text'));
console.log(chalk.blue('blue letters'), chalk.yellow('yellow letters'));
src/03_dependency/package.jsonJSON
{
  "name": "dependency-demo",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "chalk": "^5.3.0"
  }
}
src/04_cli/package.jsonJSON
{
  "name": "greet-cli",
  "version": "1.0.0",
  "type": "module",
  "bin": {
    "greet": "./bin/greet.js"
  }
}

Assignment

Assignment 1 — HR CLI

Create a CLI that inputs the name readline and outputs a greeting message.

  • Register start script in package.json.
  • Run: npm start
  • Output Hello, OOO! welcome. after input.
Show the reference solution

Try the assignment yourself first — comparing your attempt with a working solution is where most of the learning happens.

homework_01/index.jsJavaScript
/**
 *Task 1: Enter name and say hello using readline
 *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('Please enter your name:');
console.log(`Hello, ${name.trim() || 'Anonymous'}! welcome.`);

rl.close();
homework_01/package.jsonJSON
{
  "name": "greet-input",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "start": "node index.js"
  }
}

Learn this lesson with a mentor

Work through the JavaScript + Node.js track on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now