Ask questionspkg-exports: "." errors without a specified `main`

  • Version: "v12.7.0"
  • Platform: "Linux 4.4.0-17763-Microsoft" (WSL)
  • Subsystem: esm

Looking at the proposal linked-to in the recent release post, the "." syntax in exports ought to provide a default entrypoint to the package.

The current test fixture includes a "." entry, but it's not currently hooked up in the consuming module.

Adding this line to the test fixture:

  export { default as asdf } from 'pkgexports/asdf';
  export { default as asdf2 } from 'pkgexports/sub/asdf.js';
  export { default as space } from 'pkgexports/space';
+ export { default as asdf3 } from 'pkgexports';

Causes this invocation to fail:

$ node --experimental-exports --experimental-modules pkgexports.mjs
(node:939) ExperimentalWarning: The ESM module loader is experimental.
  let url = moduleWrapResolve(specifier, parentURL);

Error: Cannot find main entry point for '/c/dev/dump/node-pkg-exports/node_modules/pkgexports/' imported from /c/dev/dump/node-pkg-exports/pkgexports.mjs
    at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:59:13)
    at Loader.resolve (internal/modules/esm/loader.js:73:33)
    at Loader.getModuleJob (internal/modules/esm/loader.js:149:40)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:43:40)
    at link (internal/modules/esm/module_job.js:42:36) {

Adding a main field to node_modules/pkgexports/package.json fixes this, I'm not clear if this is the desired behaviour though. I would imagine exports should be standalone. The proposal has this to say:

For "type": "module" packages with both "main" and "exports", a main entrypoint defined by "exports" takes precedence over one defined by "main". This allows a package to be importable as either ESM or CommonJS. If a package.json lacks "exports" but includes "type": "module", "main" defines the package’s ESM entrypoint.

I think this leaves the door open on the case of having exports but not main.

Looking forward to a future with this feature!

CC: @guybedford


Answer questions jkrems

Thanks for trying this out and sorry for the confusion! :)

Github User Rank List