Skip to content

Instantly share code, notes, and snippets.

@MylesBorins
Created January 8, 2021 02:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MylesBorins/fe331cb7185e1d15d71070de0f13b9ae to your computer and use it in GitHub Desktop.
Save MylesBorins/fe331cb7185e1d15d71070de0f13b9ae to your computer and use it in GitHub Desktop.
An attempt to document exports

The "exports" field allows defining the entry points of a package. Through the use of conditional exports loader specific entries can be defined.

It can be a string an object or an array

If package.exports is a string an import or require of your package will always resolve to that string

If package.exports is an object you can define two types of fields.

Subpath export fields or conditional fields

Subpath export field specify the path that can be used by a consumer. The entry point of a module uses the subpath field ".".

Custom subpaths to the entry point can be defined using relative paths such as "./subpath".

Conditional fields can be used to define an entry point for specific loaders in Node.js or other environments. Node.js supports 4 conditional fields including "default", "import", "require", "node".

An exports object that only contains conditional fields is sugar for only defining an entry point subpath field.

Conditional fields can be composed e.g.:

"exports": {
  "node": {
    "require": "./path-to-some-file.js",
    "import": "./path-to-some-file.mjs"
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment