-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
react-use cannot be imported in Node.js #2353
Comments
I also faced this problem. Is there a temporary solution? |
I use a setup where react-use is imported by both a node module (node 16) and nextjs - node cannot use named exports reliably for CJS and if i use the default export format, then nextjs' build fails saying that there isn't a default export (because there isn't, seeing how it's node's interop that adds it). Using a namespace import leads to Node not recognizing all default re-exports. Direct file import such as import useUpdateEffect from 'react-use/lib/useUpdateEffect.js'; leads to { default: [Function: useUpdateEffect] } Needless to say, ESM CJS interop is a mess... |
I'm getting the same issue. I'm updating my app to react@18 and next@13, which automatically supports properly-packaged esm packages. However, as stated by the OP, a edit: |
This seems to happen to me to using Remix. Everything should be ESM, but I still get server-side errors trying to import the library. I'll have to move over to some other hooks library. |
Just found this issue. In case it helps somebody else, here's my workaround until the underlying problem is resolved: import useKeyLib from "react-use/lib/useKey.js";
const useKey = useKeyLib.default; |
I have the same issue. |
I am also experiencing the issue when using Remix. This import error is a real downside to using this awesome library. I hope there will be a fix soon. |
Also, for anyone using TypeScript and Remix, here's another possible workaround that extends @msurdi 's own:
|
For Vite-based projects (& Remix), this configuration should also solve the issue: // vite.config.ts
import { defineConfig } from "vite";
export default defineConfig((env) => {
return {
// […]
ssr: {
noExternal: ["react-use"],
},
optimizeDeps: {
include: ["react-use"],
},
};
}); |
For reference: https://publint.dev/[email protected] |
What is the current behavior?
I can't use ESM syntax to import
react-use
in Node.js. I found this issue when I try to run some unit tests on Node.js.The reason for this issue is that the following package.json is not supported by Node.js. There is a detailed explanation https://github.com/sheremet-va/dual-packaging
There are some simple methods to fix this issue. More explanation in the link above.
package.json
underesm/
, with the content{"type": "module"}
;.js
file underesm/
to.mjs
;.js
file underlib/
to.cjs
.Steps to reproduce it and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have extra dependencies other than
react-use
. Paste the link to your JSFiddle or CodeSandbox example below:What is the expected behavior?
react-use
can be imported in a.mjs
file with ESM syntax.A little about versions:
react-use
: 17.4.0The text was updated successfully, but these errors were encountered: