nx: @nx/rollup built library errors when using new .cjs.mjs file
Current Behavior
After upgrading from NX v16 to NX v17, the build outputs for our React packages changed. Notably the exports section changed.
// NX v16
"exports": {
"./index.css": "./index.css",
".": {
"types": "./src/index.d.ts",
"import": "./index.js", // <-- this one
"require": "./index.cjs"
}
},
// NX v17
"exports": {
"./index.css": "./index.esm.css",
".": {
"module": "./index.esm.js",
"import": "./index.cjs.mjs", // <-- this one
"default": "./index.cjs.js"
},
"./package.json": "./package.json"
},
Now on a consuming application that is using NextJS and importing via import { MODULE_NAME } from "PACKAGE", we are receiving errors that did not exist before.
export { _default as default } from './index.cjs.default.js';
^^^^^^^^^^^^^^^^^^^
SyntaxError: The requested module './index.cjs.default.js' does not provide an export named '_default'
I am not an expert in ESM or CJS, but it seems very odd to me to change how this is exported across the versions.
Expected Behavior
I would expect my consuming application to be able to consume the library without errors.
GitHub Repo
No response
Steps to Reproduce
Nx Report
> NX Report complete - copy this into the issue template
Node : 18.17.0
OS : darwin-x64
npm : 9.6.7
nx (global) : 16.5.3
nx : 17.0.1
lerna : 6.5.1
@nx/js : 17.0.1
@nx/jest : 17.0.1
@nx/linter : 17.0.1
@nx/eslint : 17.0.1
@nx/workspace : 17.0.1
@nx/cypress : 17.0.1
@nx/devkit : 17.0.1
@nx/eslint-plugin : 17.0.1
@nx/plugin : 17.0.1
@nx/react : 17.0.1
@nx/rollup : 17.0.1
@nx/storybook : 17.0.1
@nrwl/tao : 17.0.1
@nx/vite : 17.0.1
@nx/web : 17.0.1
@nx/webpack : 17.0.1
typescript : 5.1.6
---------------------------------------
Community plugins:
nx-stylelint : 15.0.0
---------------------------------------
Local workspace plugins:
@magnetic-common-design-system/workspace-plugin
Failure Logs
export { _default as default } from './index.cjs.default.js';
^^^^^^^^^^^^^^^^^^^
SyntaxError: The requested module './index.cjs.default.js' does not provide an export named '_default'
Package Manager Version
No response
Operating System
- macOS
- Linux
- Windows
- Other (Please specify)
Additional Information
No response
About this issue
- Original URL
- State: closed
- Created 8 months ago
- Reactions: 2
- Comments: 18 (2 by maintainers)
@jaysoo and I looked at the repo, it turns out the libraries have
"type": "module"in the package.json, so that is why the viest is not resolving these libraries correctly. When adding"type": "module", it will ALWAYS resolve the package as an es module, whereas in the export field, as you mentioned, the import is in CJS:Quick fixes would be removing
"type": "module"libraries’ package.json:So the consuming app will resolve these libraries correclty. i will submit a proper fix to detect when package.json has
"type": "module"and only export esm format.Thanks for the repro, we’ll take a look
@jaysoo
I have created a new repository that has this issue: https://github.com/thomassimko/nx-default-build-error
You can see the issue by doing the following:
npm iin the root directory – this installs verdaccio as a local registrynpm iin the nx directory – this is an nx component monorepo that provides components to another external appnpm run publishin the nx directory – publishes both container and types packages to verdaccionpm iin the jest-project directory – install deps for consuming appnpm run testin the jest-project directory – this runs the tests using vitest (I know I named the project poorly) and reports the error that we are seeingPlease let me know if you have any other questions. This is a big issue for our team.
This happens because when there’s only a default export, the CJS output exports it through
module.exports, notexports['default'].e.g. given
the output is
But given
the output is
To make the
defaultexport available in that case, theindex.cjs.default.jscan be adjusted as follows:@jaysoo would you like me to send a PR and/or provide a complete reproduction repo?