Concept Explanation
NPM is like an App Store for developers. If you need a utility for dates, you don't write it from scratch; you download date-fns from NPM.
The package.json file is the Inventory List of your project. It lists everything you've downloaded and provides "Shortcut Commands" (Scripts) to run your project.
Syntax or Core Structure
Package.json Anatomy
{
"name": "my-cool-app",
"version": "1.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"lodash": "^4.17.21" // Essential for the app to run
},
"devDependencies": {
"vitest": "^1.0.0" // Only needed during development
}
}
Common Mistakes
- Deleting node_modules: Beginners panic when they see this giant folder. It's okay to delete it! As long as you have your
package.json, you can just runnpm installand everything will come back. - Global vs Local: Never install project tools globally (
-g). Always install them locally in the project so that other developers can run the same versions as you. - Version Bloat: Don't install a massive library to solve a 2-line problem. Every library you add makes your website load slower for users.
Practice Exercises
- Command Master: Create a new script in a
package.jsoncalled "hello" that prints "Welcome to JS Tooling!" when you runnpm run hello. - The Lockfile: Research
package-lock.json. Why is it important to never delete this file? - Dependency Audit: Run
npm auditin any project and explain what a "Vulnerability" is in the context of NPM.