npm and package.json
The bakery wants a photo carousel and a date picker for cake orders. You could write both from scratch, or use well-tested code other developers have already shared. Packages are that shared code, and a package manager downloads them, tracks them and keeps every teammate's copy the same. npm is the one that comes with Node.js.
The rule
package.jsonlists what the project needs; the package manager fetches it.
Start a project's manifest with:
npm init -yThis creates package.json, a small JSON file describing the project:
{
"name": "crumb-site",
"version": "1.0.0",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"dependencies": {
"flatpickr": "^4.6.13"
},
"devDependencies": {
"vite": "^5.4.0"
}
}Installing packages.
npm install flatpickr # needed by the site itself
npm install -D vite # needed only while developingnpm install <name>downloads the package from the npm registry, the big public library of packages, and adds it todependencies.-D(short for--save-dev) puts it underdevDependenciesinstead: tools you need for building and testing, but which the finished site does not run.- The code lands in a
node_modulesfolder, along with every package those packages depend on. That folder gets large quickly.
Recreating the setup. A teammate who clones the repository gets package.json but not node_modules, because node_modules belongs in .gitignore. They run one command:
npm installWith no name after it, npm install reads package.json and installs everything listed.
Scripts. The scripts section gives short names to common commands. npm run dev runs vite; npm run build runs vite build. A few names such as test and start also work without run. Scripts can use tools installed in node_modules directly, so nobody has to install them globally.