Node.jsbeginner 7 min
Install other people's code with npm, and understand package.json and node_modules.
What npm is, how to start a project with package.json, and how to install and use a package.
Almost no real Node project is written from scratch. You assemble well-tested packages for dates, web servers, testing and more. npm is how you get them.
npm (Node Package Manager) comes with Node. It downloads packages from a public registry.
Key commands, run in your project folder:
Two things appear:
npm install.package-lock.json records the exact versions installed so everyone gets the same ones.
After npm init -y and npm install dayjs:
npm init -y creates package.json with default answers.npm install dayjs downloads the package into node_modules and adds it to dependencies.import dayjs from "dayjs" finds it in node_modules by name (no ./).Packages can run for a long time and change. Always check a package's download count and recent updates before depending on it.
Install the package on your own machine and run the script. The expected output is below.
node_modules. Add it to .gitignore.npm install after cloning a project.-g) instead of per project.node_modules and records them in package.json.node_modules.npm install rebuilds everything from package.json.Next: reading and writing files.
npm init -y # create package.json
npm install dayjs # download a package and record it
npm uninstall dayjs # remove itimport dayjs from "dayjs";
const d = dayjs("2025-03-14");
console.log(d.format("DD MMM YYYY"));
console.log(d.add(10, "day").format("YYYY-MM-DD"));