1

I have a problem with import data using chart.js. I am making aplications in node.js and i would like to import data from another file (use require).

When i import data then my chart not loading and console log 'require is not defined'.

const data= require('../db/controllers/data')
const ctx = document.querySelector("#myChart").getContext("2d")

let delayed

let gradient = ctx.createLinearGradient(0, 0, 0, 400)
gradient.addColorStop(0, "rgba(58,123,213,1")
gradient.addColorStop(1, "rgba(0,210,255, 0.3")

const labels = [
    "2012",
    "2013",
    "2014",
    "2015",
    "2016",
    "2017",
    "2018",
    "2019",
    "2020",
]

const data = {
    labels,
    datasets: [
        {
            data: data,
            label: "Test",
            fill: true,
            backgroundColor: gradient,
            borderColor: '#fff',
            pointBackgroundColor: 'rgb(189, 195, 199'
        },
    ],
}

const config = {
    type: "line",
    data: data,
    options: {
        radius: 5,
        hitRadius: 30,
        hoverRadius: 12,
        responsive: true,
        animation: {
            onComplete: () => {
                delayed = true
            },
            delay: (context) => {
                let delay = 0
                if( context.type === 'data' && context.mode === 'default' && !delayed) {
                    delay = context.dataIndex * 300 + context.datasetIndex * 100
                }
                return delay
            }
        },
        scales: {
            y: {
                ticks: {
                    callback: function (value) {
                        return "$" + value + "m"
                    },
                },
            },
        },
    },
}

const myChart = new Chart(ctx, config)

I try a many soultions but i don't know how i can import data from another file in Module format. Thanks you very much for your help! :)

danielo39
  • 11
  • 1
  • 1
    https://stackoverflow.com/questions/19059580/client-on-node-js-uncaught-referenceerror-require-is-not-defined Did you run your code on the browser or in node? – MTN May 20 '22 at 12:19
  • Mastermind, i run my code in the browser. In node, i using commonJS – danielo39 May 20 '22 at 20:43
  • Require is only used in node, not browser. Check this out https://nodejs.dev/learn/differences-between-nodejs-and-the-browser – MTN May 21 '22 at 21:02

0 Answers0