Home > other >  Unexpected token '{'. import call expects exactly one argument - when trying to import cha
Unexpected token '{'. import call expects exactly one argument - when trying to import cha

Time:12-15

I am creating a simple node.js app that uses chart.js for some visualisations but when trying to import and use Chart I am getting errors.

I used npm to install chart.js and served it to the client with:

app.use('/scripts', express.static(path.join(__dirname, 'node_modules/chart.js/dist')))

I am using pug as a rendering engine and have imported the scripts:

script(type="module" src="./scripts/chart.js")
script(src="/scripts/index.js")

Then in the index.js file when I try and import the chart module using:

import { Chart } from './chart.js/auto';

I get an error:

SyntaxError: Unexpected token '{'. import call expects exactly one argument.

Removing the curly braces doesn't work either.

I can see the chart.js scripts are included in the sources of the page but it will not load into the script I have tried omitting the import as I saw that inline scripts do not need this however that produces another error saying that it cannot find the variable Chart

Unsure what I am doing wrong, whether it be with serving the files or with the client-side import.

CodePudding user response:

I'm not experienced in chart.js but there's a library for chart.js for node, chartjs-node

I guess you should install it beside chart.js.

I used chart.js beside chartjs-vue to build some charts in Vue.JS previously.


install the node library with this command, and here you can find more

npm install chartjs-node

though, I guess you have to install Cairo on your system, Cairo for Canvas implementation for node.js.

you can install it with this command, here more details.

npm install canvas

CodePudding user response:

Solved

I Used a different script tag

script(src="/scripts/chart.umd.js")

Then I was not required to use any-side import statement in the client side index.js file

  • Related