Skip to content

Scope the CJS and UMD builds to CommonJS so Node and Vite SSR can import the package - #41

Open
eastagiletracker wants to merge 1 commit into
adexin:masterfrom
eastagiletracker:agile-board/fix-node-esm-import-of-cjs-builds
Open

eastagiletracker wants to merge 1 commit into
adexin:masterfrom
eastagiletracker:agile-board/fix-node-esm-import-of-cjs-builds

Conversation

@eastagiletracker

Copy link
Copy Markdown

This PR proposes making the CommonJS and UMD builds loadable from Node again, so import { SpinnerCircularFixed } from 'spinners-react' works under Vite/Astro SSR (Fixes #34). We include this PR work along with a full history of your repo at https://eastagiletracker.com/projects/1168. You can sign in with your GitHub ID to claim ownership of the project.

What was wrong

Since 1.0.11 package.json declares "type": "module", but main still points at lib/umd/index.js and the README documents require('spinners-react/lib/cjs/...'). Node decides how to parse a .js file from the nearest package.json, so it now parses the UMD and CJS bundles as ES modules: the UMD wrapper finds no exports object and exports nothing, and the CJS chunks throw require is not defined in ES module scope. Bundlers are unaffected because they follow module to lib/esm, which is why this only shows up when Node loads the package itself, as Vite does for externalized dependencies during SSR. That is exactly the setup in #34, and why the ssr.noExternal workaround in the thread helps.

The fix

rollup.config.js now emits a one-line package.json containing { "type": "commonjs" } into lib/cjs and lib/umd. Node scopes the files in those folders back to CommonJS, while the root stays "type": "module" and lib/esm keeps loading as ESM. No file is renamed and no entry point moves, so the unpkg UMD URLs, the module/types fields and every documented deep import path stay exactly as they are.

Reproduction at current master and verification

I built master, ran npm pack, and installed the tarball with react@19 into an empty project. Then node check.mjs with import { SpinnerCircularFixed } from 'spinners-react' fails with the same error as the issue:

SyntaxError: The requested module 'spinners-react' does not provide an export named 'SpinnerCircularFixed'

A CommonJS consumer gets require('spinners-react').SpinnerCircularFixed === undefined, and require('spinners-react/lib/cjs/SpinnerCircular') throws ReferenceError: require is not defined in ES module scope. With the same project on Vite 6, ssrLoadModule on a file that imports the spinner gives [vite] The requested module 'spinners-react' does not provide an export named 'SpinnerCircularFixed'.

I then repacked with this change and reinstalled. The same scripts (which also cover a deep lib/esm/SpinnerCircular.js import, lib/cjs and lib/umd deep requires, and a renderToString of the component) now print:

ESM import: function function
ESM deep import: function
<svg fill="none" style="color:#38ad48;overflow:visible;width:32px" viewBox="0 0 66 66"><ci
CJS require: function
CJS deep require: function
UMD deep require: function
vite ssr: function

npm run lint, npm run types, npx jest (10 suites, 55 tests, 19 snapshots) and npm run build all pass both before and after the change, with no new failures. The Jest suite runs against src and can't see the packed output, so the tarball check above is what covers this change. To replay it, run npm run build && npm pack, install the .tgz next to react in an empty folder, and run node -e "import('spinners-react').then(m => console.log(typeof m.SpinnerCircularFixed))".

How this was managed

This work is tracked as https://eastagiletracker.com/projects/1168/stories/1364986 on https://eastagiletracker.com/projects/1168, a board imported from this repo's issues and pull requests (40 stories) and used to manage this change.

board

If you'd rather not receive contributions like this, reply no-more-prs on this pull request and we won't open any further ones on your repositories.


Lawrence W. Sinclair
CEO / East Agile
linkedin.com/in/lwsinclair/
eastagile.com

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SpinnerCircularFixed does not exist?

1 participant