Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "module" package.json field for?

People also ask

What is the module field in package json?

The main field makes sure that Node users using require will be served the UMD version. The module field is not an official npm feature but a common convention among bundlers to designate how to import an ESM version of our library. Further discussion of pkg. module is on the Rollup Github Wiki and the webpack Docs.

What is package json used for?

The package. json file is the heart of any Node project. It records important metadata about a project which is required before publishing to NPM, and also defines functional attributes of a project that npm uses to install dependencies, run scripts, and identify the entry point to our package.

Which fields are the most important in your package json?

name — This is the most important and required field in the package. json file. This should represent the name of the project.

How do I add a module to a package json?

To add dependencies and devDependencies to a package. json file from the command line, you can install them in the root directory of your package using the --save-prod flag for dependencies (the default behavior of npm install ) or the --save-dev flag for devDependencies.


Is "module" an official npm property or is this a convention of some kind?

It's a proposal, but likely to be supported due to de facto usage.

Is there documentation for this somewhere?

There is, in fact, and it can be found right here and later removed here.

What's it for?

ES6 Module Interoperability in Node. Additional discussion can be found here and here. And here's a blog post from Rich Harris talking more about it.


This is used by bundler tools for ESM (ECMAScript Module) detection. The Rollup documentation says it pretty well:

If your package.json file also has a module field, ES6-aware tools like Rollup and webpack 2 will import the ES6 module version directly.

This article on Rollup 1.0 says it another way:

The main field makes sure that Node users using require will be served the UMD version. The module field is not an official npm feature but a common convention among bundlers to designate how to import an ESM version of our library.

Further discussion of pkg.module is on the Rollup Github Wiki and the webpack Docs.