on-screen-keyboard-detector icon indicating copy to clipboard operation
on-screen-keyboard-detector copied to clipboard

Getting an error running in Nextjs project

Open iamthesiz opened this issue 4 years ago • 8 comments

SyntaxError: Cannot use import statement outside a module

This error happened while generating the page. Any console logs will be displayed in the terminal window.

image image

I'm not familiar with @most/core

iamthesiz avatar Mar 24 '21 23:03 iamthesiz

In 861e2bcc8703f2a6975d235ff2783fd3f2930448 i.e. v2.1.0. I migrated the project to an ES6 module.

This changes some properties in package.json and should make the project more integration friendly. Especially I hope that the error above

Cannot use import statement outside module

will be fixed.

Sorry I have not used Next.js yet.

Thanks for reporting!

semmel avatar Apr 05 '21 18:04 semmel

Wait so it is fixed, or it is not fixed yet? Because I only see documentation updates.

iamthesiz avatar Apr 06 '21 01:04 iamthesiz

I would submit a PR myself but like I said, not familiar with @most/core

iamthesiz avatar Apr 06 '21 01:04 iamthesiz

Cannot use import outside a module

is clearly caused by webpack (or whatever Nextjs needs) loading oskd as commonjs. That should change if you upgrade to version 2.1.0.

I see no need to change the implementation using @most/core, since the detection works fine.

semmel avatar Apr 06 '21 07:04 semmel

Ah, I totally missed your previous commits. Thanks for the quick turnaround

iamthesiz avatar Apr 06 '21 13:04 iamthesiz

I ran into the same issue on Next.js myself, and ended up working around it by requiring the package only when rendering on the client.

useEffect(() => {
    const { subscribe } = require('on-screen-keyboard-detector')
    return subscribe(visibility => { ... })
}, [])

zhicong avatar Apr 10 '21 15:04 zhicong

@zhicong Do you mean something like this?

useEffect(() => {
  const getSubscribe = async () => {
    const keyboardDetector = await import('on-screen-keyboard-detector')
	return keyboardDetector.subscribe
  }
  const subscribe = getSubscribe()
  return subscribe(visibility => { ... })
}, [])

I'm surprised your code works. The only way your code would work is if nextjs was transpiling your code since require ins't available in the browser.

iamthesiz avatar Apr 12 '21 04:04 iamthesiz

This issue should be fixed with https://github.com/semmel/on-screen-keyboard-detector/pull/14.

Alwinator avatar Jun 18 '22 11:06 Alwinator