Topic 23 of 55
Package Manager (npm) Init
Overview
NPM is the default package manager for Node.js. Running `npm init` creates a `package.json` file, which is the foundational configuration file for any Node.js project. It tracks dependencies, scripts, and metadata.
Syntax
bash
# Start an interactive prompt to create a package.json
npm init
# Skip the prompts and generate a package.json with default values
npm init -y
# Alternatively, you can use modern package managers like pnpm or yarn
pnpm init
yarn init -yCommon Pitfalls
- Never commit the `node_modules` folder to Git. Always add it to your `.gitignore`. The `package.json` file is what you share with other developers.
- If you manually edit `package.json`, ensure it remains valid JSON. Trailing commas will break it.
Real-World Example
The workflow of initializing and installing a package:
example
bash
# 1. Create a new directory and enter it
mkdir my-app && cd my-app
# 2. Initialize the project (creates package.json)
npm init -y
# 3. Install a dependency (updates package.json and creates node_modules)
npm install express
# 4. Your project is now ready to use express!