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?
imageZoom()insideMyApp, does the error go away?process.browserbut that doesn't do any good as well.