Skip to content

Error when using Grid component in NextJS #41

@pharaxe

Description

@pharaxe

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

  1. Go to a fresh directory and run yarn create next-app
  2. Answer Y to the options (I said No to Tailwind and to import Aliases but it shouldn't matter)

image

  1. Install react-visual grid: yarn add react-visual-grid

  2. Go to the file src/app/page.tsx and 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;
  1. Run yarn dev

  2. Go to localhost:3000

Expected Behavior

Should see the photogrid load like this:

image

Actual Behavior

Next throws this error:

image

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions