Relationship between Packages and Modules

Let's cover the relationship between packages and modules in Node.js.

Let’s recap what you have learned so far:

  • A module is anything that can be loaded with require().
  • A package is a Node application described by a package.json file.

A package used in another Node application is loaded with require(), making it a module. To be loaded as a module, a package must contain an index.js file or a main field in package.json defining a specific entry point.

Some packages only contain an executable command and thus cannot be loaded as modules. On the other hand, a single JavaScript file loaded with require() is a module but not a package, since it doesn’t have a package.json file.

Check out the npm documentation for more details on this aspect.

Get hands-on with 1200+ tech skills courses.