Difference between a module and a package in Node.js?
node.jsPackagenode.js Problem Overview
I am new to Node.js. What is the difference between a "package" and a "module" in Node.js?
node.js Solutions
Solution 1 - node.js
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.
Solution 2 - node.js
A module is a single JavaScript file that has some reasonable functionality.
A package is a directory with one or more modules inside of it and a package.json file which has metadata about the package.
A package can be very simple for example, underscore just has a single JavaScript file (we see two versions of it, regular and minified version and package.json)
open@open-PC MINGW64 ~/Desktop/module-package-demo/node_modules/underscore
$ dir
LICENSE README.md underscore-min.js
package.json underscore.js underscore-min.js.map
Whereas a more complex package like Express has one JavaScript file in the root, but inside its sub-directories has quite a few more JavaScript files and more within sub-directories of that
open@open-PC MINGW64 ~/Desktop/module-package-demo/node_modules/express
$ dir
History.md index.js lib LICENSE package.json Readme.md
Now it's very common for people to refer to a package as a module.
Solution 3 - node.js
I searched the Node.js documentation and found their def for module:
> In the Node.js module system, each file is treated as a separate module.
npm has some official defs here.
> A package is a file or directory that is described by a package.json file.
> A module is any file or directory in the node_modules directory that can be loaded by the Node.js require() function.
> Note: Since modules are not required to have a package.json file, not all modules are packages. Only modules that have a package.json file are also packages.
Solution 4 - node.js
Everything what you can require()
is a module. In most cases in the CommonJS world, it's one file per module.
A package can contain several modules, but you usually load the entry point (main
), which is specified in the package.json or its index.js if no main
property is provided, for instance: require('express')
.
But you can also require another file (not the main file) if you know the location. For instance, require("express/lib/application")
(in Node.js you can omit the extension: .js).
A package can access modules from other packages if they are listed in the dependencies
property of the package.json.
Actually npm installs all the packages into node_modules which is confusing, because it should be node_packages.
Solution 5 - node.js
Every Node.js application is a package
and should have a package.json
file. Those applications act as middleware (or the equivalent of libraries) and are meant to be installed inside other applications are modules
.
In short, all modules
are packages
, but not all packages
are meant to be used as modules
, though many can be.
Modules will be installed, if listed as dependencies
in the package.json
file, and placed into the node_modules
folder, but npm
recurses through their package.json
files to add the modules
they rely on.
Solution 6 - node.js
From https://docs.npmjs.com/about-packages-and-modules:
> About packages
About modules
>
> A package is a file or directory that is described by a package.json
> file. A package must contain a package.json file in order to be
> published to the npm registry.
>
> [...]
>
>>
> A module is any file or directory in the node_modules
> directory that can be loaded by the Node.js require() function.
>
> To be loaded by the Node.js require() function, a module must be one
> of the following:
>
> * A folder with a package.json file containing a "main" field.
> * A folder with an index.js file in it.
> * A JavaScript file.
>
> ---
> Note: Since modules
> are not required to have a package.json file, not all modules are
> packages. Only modules that have a package.json file are also
> packages.
>
> ---
>
> In the context of a Node program, the module is also the
> thing that was loaded from a file. For example, in the following
> program:
>
> var req = require('request')
>
> we might say that “The variable req
> refers to the request module”.
Solution 7 - node.js
Node.js
consists of only modules
. Any file or directory is called as module
in Node.js
.
(Module)
Demo
-> app.js
(Package)
Demo
-> app.js
-> package.json
Long version:
The term package
comes from Node Package Manager
(NPM). As NPM only accepts packages
and package
is set a of files along with package.json
it has nothing to do with Node.js
. You can share your module
with other developers by publishing it as a package
(by adding pacakge.json
) in NPM registry
so that it can be used by other developers.
Similarly when you install a package
from NPM, it will be installed as a module
into the application.
In Node.js
there is no such requirement for a module
to have package.json
file. So, a module that has package.json
will be eligible to be published in the NPM registry
and it can be called as a package
.
Ex: express
is a Node.js module
which is published as a package
in NPM registry
.
npm install express --save
Above statement installs express
package
from NPM
as a module
into Node.js project.
const express = require('express')
will gives the module.export
object of express module
which is installed as a package
from NPM
.
In short:
A module
with package.json
file can be called a package.
The term package
comes from Node Package Manager(NPM). It is set of files along with package.json
and has nothing to do with Node.js
. You can share your module
with other developers by publishing it as a package
(by adding pacakge.json
) in NPM registry
so that it can be used by other developers.
Solution 8 - node.js
Package:
A package is a file or directory that is described by a package.json file. A package must contain a package.json file in order to be published to the npm registry.
Module:
A module is any file or directory in the node_modules directory that can be loaded by the Node.js require() function.
To be loaded by the Node.js require() function, a module must be one of the following:
-
A folder with a package.json file containing a "main" field.
-
A folder with an index.js file in it. A JavaScript file.
-
A module is any file or directory in the node_modules directory that can be loaded by the Node.js require() function.
In the context of a Node program, the module is also the thing that was loaded from a file. For example, in the following program:
var req = require('request')
we might say that “The variable req refers to the request module”.
Note:
Since modules are not required to have a package.json file, not all modules are packages. Only modules that have a package.json file are also packages.