0

import { UDFCompatibleDatafeed } from "./datafeeds/udf/src/udf-compatible-datafeed.js";

I need to convert the above import to the next js dynamic import. So I tried this way

const UDFCompatibleDatafeed = dynamic(() => import("./datafeeds/udf/src/udf-compatible-datafeed.js").UDFCompatibleDatafeed);

UDFCompatibleDatafeed is a javascript class.

I am getting an error

TypeError: UDFCompatibleDatafeed is not a constructor

Seems this is not importing correctly.

How do I fix this?

margherita pizza
  • 6,623
  • 23
  • 84
  • 152
  • 1
    if it is a named export do this `import('../components/hello').then((mod) => mod.UDFCompatibleDatafeed )` – ABDULLOKH MUKHAMMADJONOV Oct 08 '21 at 05:55
  • @ABDULLOKHMUKHAMMADJONOV Can I add ssr false option to import as well? – margherita pizza Oct 08 '21 at 06:09
  • Yes. ` () => import('../components/hello3'), { ssr: false }` [https://nextjs.org/docs/advanced-features/dynamic-import] – ABDULLOKH MUKHAMMADJONOV Oct 08 '21 at 06:12
  • I tried this `const widget = dynamic(() => import("./charting_library/charting_library").then((mod) => mod.widget),{ssr:false});` @ABDULLOKHMUKHAMMADJONOV seems this is not the correct implementation – margherita pizza Oct 08 '21 at 06:24
  • 1
    _"UDFCompatibleDatafeed is a javascript class"_ - Is it a React component too? `next/dynamic` should only be used to dynamically import React components. To dynamically import regular JS modules uses regular dynamic import as described in [Why am I getting ReferenceError: self is not defined in Next.js when I try to import a client-side library?](https://stackoverflow.com/questions/66096260/webpack-why-am-i-getting-referenceerror-self-is-not-defined-in-next-js-when-i/66100185#66100185). – juliomalves Oct 09 '21 at 16:51

1 Answers1

0

Use this:

const UDFCompatibleDatafeed = dynamic(
  () => import("./datafeeds/udf/src/udf-compatible-datafeed.js")
  .then(m => m.UDFCompatibleDatafeed)
);
MSKashef
  • 344
  • 2
  • 11