Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a module and a package in Node.js?

I am new to Node.js. What is the difference between a "package" and a "module" in Node.js?

like image 212
Breako Breako Avatar asked Nov 15 '13 18:11

Breako Breako


People also ask

What is node modules and package json?

The Node. js Package Manager (npm) is the default and most popular package manager in the Node. js ecosystem, and is primarily used to install and manage external modules in a Node. js project. It is also commonly used to install a wide range of CLI tools and run project scripts.

What is a module in JS?

A module in JavaScript is just a file containing related code. In JavaScript, we use the import and export keywords to share and receive functionalities respectively across different modules. The export keyword is used to make a variable, function, class or object accessible to other modules.

Are npm packages libraries?

If you are a Node. js developer, using npm packages won't be a new concept to you. From complex npm packages like express to simple packages, npm hosts a large number of Node. js libraries that simplify the workload of Node developers.

What is the difference between package and library in JavaScript?

A module is just a file containing lines of JavaScript code. A library uses one or many modules to provide a set of features. A package is a downloadable, versioned library. Think of someone putting it in a box and shipping it to you, so you can import it and use it in combination with your own code.


1 Answers

Modules are libraries for Node.js. See the below excerpt from the API:

Node.js has a simple module loading system. In Node.js, files and modules are in one-to-one correspondence.

Examples of modules:

  • Circle.js
  • Rectangle.js
  • Square.js

A package is one or more modules (libraries) grouped (or packaged) together. These are commonly used by other packages or a project of your own. Node.js uses a package manager, where you can find and install thousands of packages.

Example of a package:

Shapes             <- Package name   - Circle.js      <-   - Rectangle.js   <- Modules that belong to the Shapes package   - Square.js      <- 

Essentially, you could install the package, Shapes, and have access to the Circle, Rectangle, and Square modules.

like image 162
matth Avatar answered Sep 20 '22 00:09

matth