Within a package, the values defined in the package’s package.json ["exports"][] field can be referenced via the package’s name. For example, assuming the package.json is:

    1. // package.json
    2. {
    3. "name": "a-package",
    4. "exports": {
    5. ".": "./main.mjs",
    6. "./foo": "./foo.js"
    7. }
    8. }

    Then any module in that package can reference an export in the package itself:

    1. // ./a-module.mjs
    2. import { something } from 'a-package'; // Imports "something" from ./main.mjs.

    Self-referencing is available only if package.json has ["exports"][], and will allow importing only what that ["exports"][] (in the package.json) allows. So the code below, given the previous package, will generate a runtime error:

    1. // ./another-module.mjs
    2. // Imports "another" from ./m.mjs. Fails because
    3. // the "package.json" "exports" field
    4. // does not provide an export named "./m.mjs".
    5. import { another } from 'a-package/m.mjs';

    Self-referencing is also available when using require, both in an ES module, and in a CommonJS one. For example, this code will also work:

    1. // ./a-module.js
    2. const { something } = require('a-package/foo'); // Loads from ./foo.js.