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.
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.
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.
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)
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
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.
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.
[...]
About modules
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”.
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.
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.
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 expresspackage 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.