react-hotkeys-hook
react-hotkeys-hook copied to clipboard
Cannot use import statement outside a module
Encountering this on all v4 releases:

along with a lot of output:

How are you importing the Module?
Version 4.0.0-5
seems to work just fine: https://codesandbox.io/s/thirsty-mopsa-x43bfw?file=/src/App.tsx
If this issue persists, please provide new info, for now I am closing it due to inactivity.
Seeing this on v4.0.2, importing via Next.js 12.3.1 - looks like the default distributed module changed from CJS to ESM between 3 & 4. Which is fine, but might want to highlight "ESM only" because this'll break a lot of users setups.
Seeing this on my setup as well, with Next.js 13.0.2. @JohannesKlauss seems to be indeed about ESM modules and compatibility with Next.js
Same here, I'm using 4.0.2 and for nextjs 12.2.5 . here's a link to codesandbox that reproduces the issue. For now downgrading to 3.4.7 which solved the issue
Thanks for the reproducible, I'll check it out and come back with a fix.
I'm also seeing this problem in a Remix app. Thanks for looking into it!
Also have it 😞
@JohannesKlauss could you please ping this issue when you release a bugfix for it?
Does it still happen with version 4.0.4-0?
FYI - for the Remix folks here... Adding the package to remix.config.js
serverDependenciesToBundle
option sorts things out just fine (Remix / ESBuild will then transpile the package from ESM to CJS)
serverDependenciesToBundle: [
'react-hotkeys-hook',
],
@JohannesKlauss - I just tried out 4.0.4-0
on Remix without transpiling (see my previous comment) I'm getting an error about unexpected output (seems like its barfing on the dist/index.js
However running it it through bundler seems to straighten things out and it works fine.
Follow-up... turns out our Vitest unit tests are also failing due to this upgrade:

Only change was to bump from 3.4.7
to 4.0.3
and some minor changes to the type interfaces for our use of useHotkeys
. I'll work to get a minimal reproduction repository up for the Remix issue later today. I suspect Vitest is failing for the same reason (something's not transpiled quite right).
Thanks so much for you work on this library!
Is there a workaround that we can use for now? I'm also using Next.js 13.
I'm currently using Next.js 13 with [email protected]
.
The weirdest part is that everything works when I have useHotkeys
in one file. Yet, everything breaks as soon as I add some other useHotkeys
in another file.
I currently have:
// Shortcuts to run and submit code
useHotkeys('meta+s', () => {}, {preventDefault: true}, []);
useHotkeys('shift+enter', handleRun, {preventDefault: true, enableOnFormTags: true}, [handleRun]);
useHotkeys('meta+shift+enter', handleSubmit, {preventDefault: true, enableOnFormTags: true}, [handleSubmit]);
And I wanted to add another one in a different file:
useHotkeys('meta+shift+ArrowRight', moveToNextPage, {preventDefault: true, enableOnFormTags: true}, [moveToNextPage]);
If I add this to the same initial file it works fine, but as soon as I add it to another file the whole thing breaks.
I have a PR in (#839) to fix this. I personally needed the fix ASAP so I published a fork on NPM with the changes: @nicksrandall/react-hotkeys-hook
@nicksrandall Thanks for the PR! I added a few comments. Maybe you can have a look at it before we can merge it
Hey all, based on @nicksrandall work, I have released version 4.0.4-1
Please check if the issue still persists. For me it doesn't seem to change everything, so I'll drop esbuild and go back to rollup.
Hey, so with 4.0.4-5 the problems should be gone, please check this version out and give me feedback if the issue is solved.
👍 Can confirm, 4.0.4-5 works in my setup, thanks!
Ditto. Working for me as well. Thank you
Can confirm for 4.0.4-5
, works like a charm. Thanks @JohannesKlauss @nicksrandall !
I published version 4.0.4 as a stable patch. Thanks for the patience and help everybody