Next: Nodejs Modules, Previous: Package Entry Points, Up: Node Packages [Index]
This section describes the fields used by the Node.js
runtime. Other tools
(such as npm
) use additional fields which are ignored by Node.js
and not
documented here.
(string)
Relevant when using named imports within a package. Also used by package managers as the name of the package.
(string)
The default module when loading the package, if exports is not specified,
and in versions of Node.js
prior to the introduction of exports. The
"main" field defines the script that is used when the package directory is
loaded via require()
. Its value is a path.
require('./path/to/directory'); // This resolves to ./path/to/directory/main.js.
(string)
The package type determining whether to load .js
files as ‘CommonJS’ or
‘ES’ modules. The "type" field defines the module format that Node.js
uses
for all .js
files that have that package.json
file as their nearest
parent.
The nearest parent package.json
is defined as the first package.json
found when searching in the current folder, that folder’s parent, and so on
up until a node_modules
folder or the volume root is reached.
(Object | string | string[])
Package exports and conditional exports. When present, limits which
submodules can be loaded from within the package. The "exports" field allows
defining the entry points of a package when imported by name loaded either
via a node_modules
lookup or a self-reference to its own name.
(Object)
Package imports, for use by modules within the package itself.