Courses / Node.js: From First Script to Production / Chapter 8
Module 5 · Packages, databases and advanced topics · Chapter 8 of 27
npm and Package Management: Dependencies, Versions and Lockfiles
Builds on Globals and Modules: process, CommonJS and ES Modules. Next up: Working with Databases: A REST API on PostgreSQL.
Part 1 · What npm is and package.json
A registry and a command
Almost every Node project you will see imports code that someone else wrote, and npm is how that code gets onto your machine. This lesson covers what npm is and the one file every project has, package.json.
The word npm names three things. There is a registry, a public online store of packages at npmjs.com. There is the npm command line tool, which comes with Node and talks to the registry. And there are the packages themselves: folders of code with a manifest file that says what they are. A package can be a library you import, a command you run, or both.
Every package has a file called package.json at its top. It is the manifest: its name, its version, the packages it needs and the commands to run it. Your own project is a package too, even if you never publish it, because npm reads package.json to learn what your project needs and installs from it.
Package names live in one shared space on the registry. A name must be 214 characters or fewer, must be lower case for new packages, and may use only characters that are safe in a URL. A scoped package puts the owner first, as in @fest/ui, which avoids clashes between different owners.