Skip to main content

Programming concepts

What is a Library?

Definition

A library is a collection of reusable code that your program calls, leaving your own code in control of the overall flow.

Also known as: Package, Module

Library in more detail

A library solves a bounded problem — parsing dates, drawing charts, talking to a database, hashing passwords — and exposes functions or classes you invoke when you need them. Nothing about it dictates how your application is arranged. You can adopt one for a single function, use three that know nothing about each other, and remove one without restructuring anything around it.

That is precisely what separates a library from a framework: who is calling whom. Your code calls a library; a framework calls your code. Size has nothing to do with it — a large charting library is still a library, and a small framework that owns your entry point is still a framework. Some packages sit on the line, and React is the usual example: presented as a library, yet it decides when your components run.

The practical concerns are dependency ones. Every library is code you did not write and are now responsible for shipping: it carries its own transitive dependencies, its own release cadence and its own security advisories. Small utilities are often cheaper to write than to depend on, while cryptography, authentication and date-and-time handling are exactly where a well-maintained library beats anything you would write yourself.

Library example

You are in control — call it where you like

  import { z } from "zod";                 # validation
  import bcrypt from "bcrypt";             # password hashing
  import { format } from "date-fns";       # dates

  const Schema = z.object({ email: z.string().email() });
  const parsed = Schema.parse(input);            // your flow
  const hash   = await bcrypt.hash(password, 12);// your order
  const label  = format(new Date(), "d MMM yyyy");

Three libraries that know nothing about each other, used in
an order your code chose. Swap any one out and the rest
carry on unchanged.

A framework would have decided the file, the function
signature and the moment all three ran.

Library — common questions

What is the difference between a library and a framework?
Who calls whom. Your code calls a library at moments you choose; a framework owns the application's flow and calls your code. Everything else — size, popularity, feature count — is incidental.
Is a package the same as a library?
Nearly, in everyday use. A package is the distribution unit an installer fetches; a library is the reusable code inside it. One package can contain several modules, and some packages ship tools rather than a library at all.
When should I write code myself instead of adding a library?
When the task is small and well understood, and the dependency would bring a large tree of transitive packages you must then keep patched. For cryptography, authentication, parsing and date handling, the reverse holds — use the maintained library.

Keep reading

Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.

Browse the A-Z glossary