-
Notifications
You must be signed in to change notification settings - Fork 4
Description
Summary
When importing the component, NextJS seems to choose the wrong JavaScript module file. This might be a bug with the way the library is packaged - OR it might just be me not setting up Next right. But I figured I'd post a issue in case others are running into this.
If I try to import it normally like so:
import { Grid } from "react-visual-grid";
I get this error:
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/home/bsweedler/grid-test/node_modules/react-visual-grid/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.``
I can get the component to render correctly if I import it like this
import { Grid } from "react-visual-grid/dist/react-visual-grid.esm";
But that's not ideal since TypeScript doesn't give nice type completions if I do this.
Steps to Reproduce
This is reproducible in a fresh NextJS app:
The Node version I'm using is 18.3
- Go to a fresh directory and run
yarn create next-app - Answer Y to the options (I said No to Tailwind and to import Aliases but it shouldn't matter)
-
Install react-visual grid:
yarn add react-visual-grid -
Go to the file
src/app/page.tsxand replace it's content with the example from the docs:
"use client";
import { Grid } from "react-visual-grid/dist/react-visual-grid.esm";
import "react-visual-grid/dist/react-visual-grid.css";
const App = () => {
const images = Array.from({ length: 30 }, (_, index) => ({
src: `https://picsum.photos/id/${index + 1}/800/600`,
alt: `Image ${index + 1}`,
}));
return (
<div>
<Grid images={images} width={800} height={600} />
</div>
);
};
export default App;
-
Run
yarn dev -
Go to localhost:3000
Expected Behavior
Should see the photogrid load like this:
Actual Behavior
Next throws this error:


