Home Forums StockChart Support Using with nextJs

Using with nextJs

Viewing 3 posts - 1 through 3 (of 3 total)
  • #34496

    Is there any way to use canvasJs with nextJS ?
    I tried to use canvasjs react stockchart in next app and got an error — “document is not defined”



    The error “document is not defined” is thrown because document is only available inside the browser and not on the server. Next js executes the code on server side and that’s why the error is thrown.

    As a solution, you can use dynamic imports to import a chart component and pass ssr: false as the second argument. This way the imported module will only run inside the context of the browser.

    For example if you want display the Chart as a component then import it dynamically as shown below :

    const StockChart = dynamic(() => import("./Chart/StockChart"), { ssr: false });

    Then use it like a component – <StockChart />

    Please take a look at this working sample project to integrate CanvasJS in Next.js. Also, kindly refer to the instructions.txt file for steps to deploy the sample.

    CanvasJS StockChart in Next.js

    Indranil Deo
    Team CanvasJS


    Thank you, it worked for me.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.