DEV Community

Discussion on: What is the difference between packages and modules?

Collapse
 
rhymes profile image
rhymes

Ok then packages is not exactly a JavaScript concept, it's just the name that we give to the unit of distribution through npm/yarn.

Libraries or software that gets "packaged", distributed, indexed in a common website (the npm repository) and then can be installed from a client. npm or yarn are the package managers, the piece of software that you add as a dependency in your application is called a package.

JavaScript modules are instead a recent addition to the standard. They allow you to organize code in files and import them from one another, encapsulated in a namespace (instead of having everything global like in the old days). So you might have a module that contains common behavior and import it from multiple files. Es:

// time.js
export function now() {}
export function yesterday() {}
Enter fullscreen mode Exit fullscreen mode

and then in your app you can import:

import { now, yesterday } from "./time.js"
console.log(now())
Enter fullscreen mode Exit fullscreen mode

I think this is the simplest explanation I can make, if you want to know more, this is a complete overview on how JavaScript modules work: developers.google.com/web/fundamen...

Thread Thread
 
jebuscraisp profile image
jebuscraisp

Very nice. Thank you