NPM sources contain packages, which are also node modules, or contain node modules. Read on to learn how they differ and how they interact.

About Packages

Packages are files or directories described in package.json files. Packages must contain package.json files to be published to the NPM registry. For more information about creating package.json files, “Create package.json files.”

Packages can be unscoped or scoped to users or organizations. Scoped packages can be private or public. For more information see: 1. about scope 2. about private packages 3. package scope, access level, and visibility

About Package Formats

A package means any of the following:

  • a) A folder containing a program described by a package.json file.
  • b) A gzipped tarball containing (a).
  • c) A URL that resolves to (b).
  • d) A @ that is published on the registry with (c).
  • e) A @ that points to (d).
  • f) A that has a latest tag satisfying (e).
  • g) A git url that, when cloned, results in (a).

Git URL format of NPM package

Git urls for NPM packages can be in the following format:

  • git://github.com/user/project.git#commit-ish
  • git+ssh://user@hostname:project.git#commit-ish
  • git+http://user@hostname/project/blah.git#commit-ish
  • git+https://user@hostname/project/blah.git#commit-ish

Commit-ish can be any tag, SHA, or branch that can be used as a Git checkout parameter; By default, commit-ish is master.

About the module

A module can be any file or folder in the node_modules folder, which can be loaded by node.js’s require() function. In order to be loaded by Node.js’s require() function, a module must be one of the following:

  • The package.json file, which contains the “main” field.
  • Js file

Note: Modules do not have to have a package.json file. Not all modules are packages, and modules that contain package.json files are packages.

In the context of a Node program, modules are also loaded from a file. For example, in the following program:

var req = require('request')
Copy the code

We can say “variable req references module”.