package.json reference
main
Specifies the main entry point module for your package. When a user imports or requires your package, the module specified in the main
field will be presented to them by default.
For example, if your package.json
has the following fields:
{
"name": "sajad-package",
"version": "1.0.0",
"main": "index.js"
}
When a user uses your packages like this
const sajadPackage = require('sajad-package');
Node.js will look inside the node-modules/sajad-package
directory for the package.json
file, read its main
field, and then load the index.js
file.
If the package.json
file doesn’t have a main
field, Node.js will default to looking for index.js
in the root of the package directory.
You’ll also see some packages with fields for module
and browser
. These specify entry points for different module systems or environments and are used by tools like Webpack and Rollup when they bundle files.
type
This field determines how .js
files within the context of the package (files specified in the files
field) should be interpreted: either as CommonJS modules or ECMAScript (ESM) modules.
Possible values:
commonjs
(default): Files will be interpreted as CommonJS modules. To use ECMAScript modules the files must have the.mjs
extension.module
:.js
files will be interpreted as ESM modules. To use CommonJS files, you’ll need to use the.cjs
extension for the files.
Sources / further reading
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment