react-hotkeys-hook icon indicating copy to clipboard operation
react-hotkeys-hook copied to clipboard

Cannot use import statement outside a module

Open colinricardo opened this issue 2 years ago • 2 comments

Encountering this on all v4 releases:

CleanShot 2022-08-23 at 21 41 36@2x

along with a lot of output:

CleanShot 2022-08-23 at 21 42 11@2x

colinricardo avatar Aug 23 '22 20:08 colinricardo

How are you importing the Module?

JohannesKlauss avatar Aug 24 '22 12:08 JohannesKlauss

Version 4.0.0-5 seems to work just fine: https://codesandbox.io/s/thirsty-mopsa-x43bfw?file=/src/App.tsx

JohannesKlauss avatar Aug 25 '22 08:08 JohannesKlauss

If this issue persists, please provide new info, for now I am closing it due to inactivity.

JohannesKlauss avatar Nov 04 '22 14:11 JohannesKlauss

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.

tmcw avatar Nov 04 '22 16:11 tmcw

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

Rednegniw avatar Nov 05 '22 11:11 Rednegniw

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

jkesteris avatar Nov 05 '22 13:11 jkesteris

Thanks for the reproducible, I'll check it out and come back with a fix.

JohannesKlauss avatar Nov 07 '22 09:11 JohannesKlauss

I'm also seeing this problem in a Remix app. Thanks for looking into it!

nicksrandall avatar Nov 07 '22 15:11 nicksrandall

Also have it 😞

@JohannesKlauss could you please ping this issue when you release a bugfix for it?

ZuBB avatar Nov 07 '22 22:11 ZuBB

Does it still happen with version 4.0.4-0?

JohannesKlauss avatar Nov 08 '22 16:11 JohannesKlauss

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',
  ],

lukebowerman avatar Nov 08 '22 16:11 lukebowerman

@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.

lukebowerman avatar Nov 08 '22 17:11 lukebowerman

Follow-up... turns out our Vitest unit tests are also failing due to this upgrade:

image

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!

lukebowerman avatar Nov 08 '22 17:11 lukebowerman

Is there a workaround that we can use for now? I'm also using Next.js 13.

MartinXPN avatar Nov 09 '22 09:11 MartinXPN

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.

MartinXPN avatar Nov 10 '22 07:11 MartinXPN

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 avatar Nov 14 '22 17:11 nicksrandall

@nicksrandall Thanks for the PR! I added a few comments. Maybe you can have a look at it before we can merge it

JohannesKlauss avatar Nov 16 '22 16:11 JohannesKlauss

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.

JohannesKlauss avatar Nov 16 '22 16:11 JohannesKlauss

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.

JohannesKlauss avatar Nov 16 '22 17:11 JohannesKlauss

👍 Can confirm, 4.0.4-5 works in my setup, thanks!

tmcw avatar Nov 16 '22 18:11 tmcw

Ditto. Working for me as well. Thank you

lukebowerman avatar Nov 16 '22 19:11 lukebowerman

Can confirm for 4.0.4-5, works like a charm. Thanks @JohannesKlauss @nicksrandall !

MartinXPN avatar Nov 17 '22 08:11 MartinXPN

I published version 4.0.4 as a stable patch. Thanks for the patience and help everybody

JohannesKlauss avatar Nov 17 '22 08:11 JohannesKlauss