Import errors from react-hook-form in Vite library (wrapper library for ShadCN UI) #11524
Replies: 2 comments 5 replies
-
UpdateI did some fiddling around directly with When removing the "exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.ts",
"react-server": "./dist/react-server.esm.mjs", // <-- Remove this
"import": "./dist/index.esm.mjs",
"require": "./dist/index.cjs.js"
}
}, So it seems that the errors are not caused by ESM/CJS but somehow by this QuestionThis obviously raises the question of what this entry is for and why it is breaking my build when consumed "through" my library in an Next JS app router app. In the node documentation about those conditional exports, I only see that it supports some predefined keywords ( Also, is there anything I can change in my consuming app or library to make this work? FYI, this is not an issue when directly importing |
Beta Was this translation helpful? Give feedback.
-
SolutionI found a solution to this and described it in this answer to my question on SO. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm currently writing a reusable UI library with Vite which is a simple wrapper around ShadCN Uis components. ATM I'm only using the library in a couple of NextJSs apps and everything is working as expected when only using the library in pages router components but I'm getting import errors when using it in app router components.
The errors are caused by imports (& re-exports) from third party dependency
react-hook-form
in the library.In detail I'm getting the following when running
npm run build
:Here's some excerpt from
index6.js
withinnode_modules
of the consuming app which is mentioned in the error:There are a few interesting things for which I don't really have an explanation:
Slot
from@radix-ui/react-slot
is working as expected even though I'm treating the dependencies to@radix-ui/react-slot
andreact-hook-form
the same way (seepackage.json
later).node_modules
folder in my consuming app I can see thatreact-hook-form
is installed and theFormProvider
is exported fromreact-hook-form
as expected.react-hook-form
directly in my consuming app but as soon as I'm using any component from the library.Implementation details
library/vite.config.ts
library/package.json
library/src/components/form.tsx
The Source which holds the troublesome imports & re-expors from
react-hook-form
):consuming-app/src/app/rsc/page.tsx
Some wild guesses I have
I explicitly set my library to "ESM only" as trying to export both CJS & ESM was causing issues in the consuming apps which I wasn't able to solve otherwise. Maybe this is causing the issue?
When comparing
@radix-ui/react-slot/package.json
(which is working fine) &react-hook-form/package.json
(which causes issues) I saw thatreact-slot
doesn't provide anycjs
files whilstreact-hook-form
does providecjs
&esm
files.Are my consuming apps (or the library itself) maybe trying to import the
cjs
files fromreact-hook-form
instead of theesm
files? If so, how can I fix this?Reproduction
You can download both, a minimal example of the library and a test consuming app here:
The issues can reproduced by running
npm i && npm run dev
ornpm run build
in the consuming app.Conclusion
I'm rather new to library authoring and am still having trouble understanding the ins- & outs of ESM & CJS and all implications of choosing one and/or the other.
Also, I'm not sure if the issue is caused by my library, the consuming apps or the third party dependencies.
Anyhow, I'd really appreciate some help with this and would be happy to provide more information if needed.
FYI, I also asked this question on Stackoverflow but wanted to try my look here as well as the errors are directly related to
react-hook-form
whilst I'm not sure whether this is a bug or just some incorrect usage of mine.Beta Was this translation helpful? Give feedback.
All reactions