Creating a New Project
The npm init command creates a new package.json file for your project. It's the first step in starting any Node.js project.
Basic npm init
Run this command in an empty directory:
npm init
npm will ask you a series of questions:
package name: (my-project)
version: (1.0.0)
description: My awesome project
entry point: (index.js)
test command: jest
git repository:
keywords: web, api
author: Your Name
license: (ISC) MIT
After answering, npm shows you the generated package.json and asks for confirmation.
Quick Init with Defaults
Skip all questions and accept defaults:
npm init -y
# or
npm init --yes
This creates a minimal package.json:
{
"name": "my-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Setting Default Values
Configure defaults so npm init -y uses your preferred values:
npm config set init-author-name "Your Name"
npm config set init-author-email "you@example.com"
npm config set init-license "MIT"
npm config set init-version "0.1.0"
Now npm init -y will use these values automatically.
Creating Specific Project Types
Create a Private Package
For projects you don't want to publish:
npm init -y
Then add to package.json:
{
"private": true
}
This prevents accidental publishing to npm.
Create an ES Module Project
For projects using ES modules (import/export):
{
"name": "my-esm-project",
"version": "1.0.0",
"type": "module"
}
Create a CLI Tool
For command-line tools:
{
"name": "my-cli",
"version": "1.0.0",
"bin": {
"my-cli": "./bin/index.js"
}
}
npm init with Templates
Use templates to scaffold specific project types:
# Create a React app
npm init react-app my-app
# Create a Vite project
npm init vite@latest my-project
# Create an Express app
npm init express-generator my-api
These run npm exec create-<initializer> behind the scenes.
Practice: Create package.json
Here's what npm init -y produces. Try customizing it:
Exercise: Add the following:
- A meaningful description
- Your name as author
- A "start" script that runs
node index.js - Change the license to "MIT"
Project Structure After Init
After npm init, a typical project structure looks like:
my-project/
├── package.json # Created by npm init
├── index.js # Your main file (create this)
├── node_modules/ # Dependencies (created later)
└── package-lock.json # Lock file (created later)
Common Workflow
Here's a typical workflow for starting a new project:
# 1. Create project directory
mkdir my-project
cd my-project
# 2. Initialize npm
npm init -y
# 3. Install dependencies
npm install express
# 4. Create your main file
touch index.js
# 5. Start coding!
Best Practices
1. Always Use Version Control
Initialize Git alongside npm:
git init
npm init -y
2. Add a .gitignore
Create .gitignore to exclude node_modules:
node_modules/
.env
.DS_Store
3. Start with a README
Document your project from the beginning:
echo "# My Project" > README.md
4. Use Semantic Versioning
Start at 0.1.0 for projects in development, 1.0.0 for production-ready code.
Key Takeaways
- npm init creates your package.json interactively
- npm init -y uses defaults for quick setup
- Configure defaults with
npm config set init-* - npm init <template> scaffolds specific project types
- Always add a .gitignore to exclude node_modules
What's Next?
In the next lesson, we'll explore all the essential fields you can add to your package.json to make your project complete.

