Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NextJS error when importing SmartEdge and SmartEdgeProvider #18

Closed
AnhHuy02 opened this issue Apr 7, 2022 · 5 comments
Closed

NextJS error when importing SmartEdge and SmartEdgeProvider #18

AnhHuy02 opened this issue Apr 7, 2022 · 5 comments
Labels
invalid This doesn't seem right

Comments

@AnhHuy02
Copy link

AnhHuy02 commented Apr 7, 2022

In NextJS 12.1.0, I used React Flow v10 with SmartEdge v0.5.0, and tried to import like your document

import ReactFlow, {
  addEdge,
  Background,
  useNodesState,
  useEdgesState,
  MiniMap,
  Controls,
  Node,
} from "react-flow-renderer";
import { SmartEdge, SmartEdgeProvider } from "@tisoap/react-flow-smart-edge";

//.......................

const edgeTypes = {
  smart: SmartEdge,
};

//.......................

const WorkflowContainer = () => {
  //..........................

  return (
      <SmartEdgeProvider options={{ debounceTime: 300 }}>
        <ReactFlow
          className="react-flow-subflows-example"
          nodes={nodes}
          edges={edges}
          onNodeMouseEnter={(
            event: MouseEvent<Element, globalThis.MouseEvent>,
            node: Node<any>
          ) => {
            handleNodeMouseEnter(event, node);
          }}
          // onNodeDragStop
          onNodesChange={onNodesChange}
          onEdgesChange={onEdgesChange}
          nodeTypes={nodeTypes}
          edgeTypes={edgeTypes}
          // onConnect={onConnect}
          fitView
        >
          <MiniMap />
          <Controls />
          <Background />
        </ReactFlow>
      </SmartEdgeProvider>
  );
};

export default WorkflowContainer;

But the browser showed the error:

Error: require() of ES Module D:\digi-curriculum\digi-curriculum-frontend\node_modules\react-flow-renderer\dist\esm\index.js from D:\digi-curriculum\digi-curriculum-frontend\node_modules@tisoap\react-flow-smart-edge\dist\react-flow-smart-edge.cjs.development.js not supported.

Instead change the require of index.js in D:\digi-curriculum\digi-curriculum-frontend\node_modules@tisoap\react-flow-smart-edge\dist\react-flow-smart-edge.cjs.development.js to a dynamic import() which is available in all CommonJS modules.

@AnhHuy02
Copy link
Author

AnhHuy02 commented Apr 8, 2022

After spending time on Next.JS, I think I fixed this error

Instead doing 1 line import

import { SmartEdge, SmartEdgeProvider } from "@tisoap/react-flow-smart-edge";

I changed to this:

import dynamic from "next/dynamic";

const SmartEdgeProvider = dynamic(
  async () => {
    const { SmartEdgeProvider } = await import("@tisoap/react-flow-smart-edge");
    return SmartEdgeProvider;
  },
  { ssr: false }
);
const SmartEdge = dynamic(
  async () => {
    const { SmartEdge } = await import("@tisoap/react-flow-smart-edge");
    return SmartEdge;
  },
  { ssr: false }
);

You need to update the document for NextJS.

@tisoap
Copy link
Owner

tisoap commented Apr 9, 2022

Hey @AnhHuy02 , thanks for figuring out how to make this library work under Next.js! I'll test this first before making changes to readme

@tisoap tisoap added the documentation Improvements or additions to documentation label Apr 9, 2022
@tisoap
Copy link
Owner

tisoap commented May 8, 2022

@AnhHuy02 I just tested it under Next.js and could not replicate this problem, I was able to use named imports with no errors on the browser. Could you share a minimal example on CodeSandbox or a minimal GitHub repo where the error happens?

@AnhHuy02
Copy link
Author

AnhHuy02 commented May 9, 2022

Here: https://codesandbox.io/s/trusting-pare-294d7t?file=/components/app.js

For some reason, I did one line import and it still work. That's weird.
Maybe if a large complex dataset will cause the path finding function in your library take more time to execute.

@tisoap
Copy link
Owner

tisoap commented May 16, 2022

I still saw no error in the sandbox you sent. I noticed you're using older versions of both react-flow-renderer and @tisoap/react-flow-smart-edge, please upgrade to their respective latest versions and try again. Here's a working CodeSandbox: https://codesandbox.io/s/smart-edge-nextjs-xx6oo4

@tisoap tisoap closed this as completed May 16, 2022
Repository owner locked as resolved and limited conversation to collaborators May 16, 2022
@tisoap tisoap added invalid This doesn't seem right and removed documentation Improvements or additions to documentation labels May 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants