completefrontend

What's the difference between a library and a framework?

Your code calls a library, but a framework calls your code. Here's the difference with real examples, a side-by-side table, and where React, Next.js, Angular, and Vue fit.

Two arrows: your code calls a library, and a framework calls your code

When you rent a car, you're the driver. You choose the route, the stops, and when to leave. On a train, the railway runs the trip. You pick a seat, but the train decides the route and when each stop comes.

The difference between a library and a framework is who's in charge: your code calls a library when it needs one, but a framework calls your code. A library is like the rental car. It's ready-made code that does what your code asks, when your code asks. A framework is like the train. It's a structure your app lives inside, and it calls the pieces you write at the right moments.

That one idea settles the library vs framework question, and the rest of this post shows it with real code.

What is a library?

A library is a collection of ready-made code that solves one kind of problem. You add it to your project and call its functions when you need them. Your code decides when and how.

Here's Lodash, a library of small helpers, removing repeated items from a list:

import { uniq } from "lodash-es";

const tags = uniq(["css", "html", "css"]);
console.log(tags); // ["css", "html"]

Your code calls uniq, gets an answer back, and carries on. Lodash never runs anything on its own. If you removed it, you'd replace one line, not rebuild your app.

Other libraries you'll meet include date-fns for dates, Chart.js for charts, and jQuery for changing the page.

What is a framework?

A framework is a ready-made structure for a whole app. It has rules for where your code goes and what shape it takes. You fill in the pieces, and the framework decides when to run them.

Here's a page in Next.js, a framework built on React:

// app/about/page.jsx
export default function AboutPage() {
  return <h1>About us</h1>;
}

Nothing in your code calls AboutPage. Next.js sees a file named page.jsx in a folder named about. So when someone visits /about, Next.js runs this component and shows what it returns. You wrote the piece, and the framework decided when it runs.

What is the main difference between a library and a framework?

It comes down to control. With a library, your code is in charge and calls the library. With a framework, the framework is in charge and calls your code. Programmers call this flip inversion of control. It's also known by a line from Hollywood: "Don't call us, we'll call you."

Two columns compare who calls whom. With a library, your code calls the library and gets an answer back. With a framework, the framework calls the code you wrote when it decides to.
Who calls whom is the real difference

Here's how the two compare:

Library Framework
Who's in charge Your code The framework
Who calls whom You call the library The framework calls your code
What it covers Usually one job, like dates or charts The structure of a whole app
Rules for your code Few, so you use it however you like Many, so files and functions go where it expects
Swapping it out Often a small change Usually means rewriting much of the app
Examples Lodash, date-fns, jQuery, React Angular, Next.js, Nuxt

Size doesn't decide it. A big library is still a library, and a small framework is still a framework. What matters is who's in charge.

Is React a library or a framework?

React is a library. Its own website calls it "the library for web and native user interfaces." React's job is the user interface: turning components and data into what's on the screen, and updating the screen when the data changes. Why React? shows how it does that.

React does call your component functions, so it has a little framework in it. But it stops at the interface. It doesn't decide which page shows for each address, how data loads, or how the app is built for release.

That's why React's docs recommend starting a new app with a framework built on React, such as Next.js or React Router. The React frameworks lesson lists what React leaves to you and compares the frameworks that fill the gap.

Is Next.js a library or a framework?

Next.js is a framework. It's built on React and adds what a whole app needs. That includes routing, which means choosing the page for each address, plus HTML made on the server, data loading, and a build step. You put files where Next.js expects them, and it does the rest.

Next.js vs React compares the two side by side. Is Next.js frontend or backend? explains the server side of it. The Next.js course teaches it from the start.

Is Angular a library or a framework?

Angular is a framework, and it covers the whole app. It comes with its own router, forms, a tool for calling APIs, and a command-line tool that creates and builds projects. Your components are classes that Angular creates and runs.

Vue sits in between. It calls itself "the progressive framework." You can drop it into one corner of a page, the way you'd use a library. Or you can add its official router and state library and let it run a whole app. React vs Angular vs Vue compares all three.

Should you use a library or a framework?

Use a library when you need one job done inside code you control. A chart on a dashboard or a date on a receipt doesn't need a framework.

Use a framework when you're building a whole app and want the common choices made for you. A framework handles routing, data loading, and the build in one tested way. That leaves you more time for your app's own features.

You'll often use both. Frameworks are built from libraries, like Next.js on React, and you'll add more libraries inside them. The question is never really "one or the other." It's which parts you want to control yourself.

What mistakes do beginners make here?

  • Calling everything a framework. People often say "JavaScript frameworks" for any popular tool. Asking who calls whom gives you the real answer.
  • Picking a framework before learning the language. Every framework here is JavaScript underneath. When something breaks, you'll fix it with JavaScript, so learn the basics first.
  • Fighting the framework. A framework works best when you follow its rules. If you keep working around them, a set of libraries may suit the project better.

Where should you go next?

To see a library from the inside, build a mini library, a small jQuery-style one, in the JavaScript course. To see the other side, build a reactive UI makes a tiny framework in one page. Then start the React course with Why React?, and move on to the Next.js course when you want a full framework.

Written by Shrikanth PS

Senior Software Engineer at Microsoft, working on Copilot Enterprise, with more than nine years in frontend engineering.