profile
viewpoint

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.

https://github.com/nodejs/node/blob/dcb692918348986945cbd4fbce8158b2b97a8418/test/fixtures/node_modules/pkgexports/package.json#L2-L3

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.
internal/modules/esm/default_resolve.js:59
  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) {
  code: 'ERR_MODULE_NOT_FOUND'
}

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

nodejs/node

Answer questions jkrems

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

useful!

Related questions

Crash with "req.handle.writev is not a function" on Socket.Writable.uncork hot 2
--max-http-header-size= is not allowed in NODE_OPTIONS hot 2
Assertion `(parser->current_buffer_len_) == (0)' failed hot 1
shutdown ENOTCONN on TLS.Socket._final hot 1
Node.js 12.10 throwing EPROTO on HTTPS request hot 1
Remove util.inherits usage internally? hot 1
[Bug] Node 10.1.0 TLS issue with ldap: Client network socket disconnected before secure TLS connection was established hot 1
ReferenceError: internalBinding is not defined hot 1
HTTP/2 requests eventually start throwing NGHTTP2_ENHANCE_YOUR_CALM errors hot 1
Incorrect timezone hot 1
HPE_INVALID_HEADER_TOKEN on http requests hot 1
HPE_INVALID_HEADER_TOKEN on http requests hot 1
crypto, bad decrypt hot 1
stream.finished behaviour change hot 1
display node.js version (process.version) at the end of stacktraces hot 1
Github User Rank List