0

I am trying to use fast-image-zoom in my React/Next.js app & I get the following error:

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:353:18)
    at wrapSafe (node:internal/modules/cjs/loader:1039:15)
    at Module._compile (node:internal/modules/cjs/loader:1073:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
    at Module.load (node:internal/modules/cjs/loader:989:32)
    at Function.Module._load (node:internal/modules/cjs/loader:829:14)
    at Module.require (node:internal/modules/cjs/loader:1013:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.fast-image-zoom (my-app\.next\server\pages\_app.js:20969:18)
    at __webpack_require__ (my-app\.next\server\webpack-runtime.js:25:42)
my-app\node_modules\fast-image-zoom\src\index.js:1
import styles from './styles'
^^^^^^

I tried calling it as is in _app.tsx:

import imageZoom from 'fast-image-zoom';

imageZoom();

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />;
}

export default MyApp;

But it threw the above error as it is a pure ESM module.

Then I tried using it in _document.tsx like:

async componentDidMount() {
    dynamic(() => import('fast-image-zoom').then((mod) => mod.imageZoom), { ssr: false })
}

But then I have no error but it doesn't work.

I've made a reproduction here -> https://stackblitz.com/edit/nextjs-edw9pk?file=pages%2F_app.js

You can uncomment the 2 lines in pages/_app.js to see the errors.

How can I solve it?

2
  • When you call imageZoom() inside MyApp, does the error go away? Commented Aug 16, 2021 at 10:55
  • @SinanYaman nope as the module is pure ESM so I need a check for it to only work in client-side. I tried using process.browser but that doesn't do any good as well. Commented Aug 16, 2021 at 11:08

1 Answer 1

1

Change your file to below

_app.js code

import { useEffect } from 'react';

function MyApp({ Component, pageProps }) {
  useEffect(() => {
    const loadImageZoom = async () => {
      const imageZoom = await import('fast-image-zoom').then(
        mod => mod.default
      );
      imageZoom();
    };
    loadImageZoom();
  }, []);

  return <Component {...pageProps} />;
}

export default MyApp;
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you that works. Is there any reason the first time I click on zoom, it is reasonably slow? Only on my app, the Stackblitz works fine :)
No idea regarding that as I haven't tried the library myself. But you could check chrome dev tools and do a profiling which could give you insights on improving that part.
Never did profiling so let me know if you have any recommended resources to get started :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.