Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected token '{'. import call expects exactly one argument - when trying to import chart.js

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.

like image 641
dosfr332 Avatar asked Nov 19 '25 17:11

dosfr332


1 Answers

Solved

I used a different link in the script tag. Instead of /chart.js I used /chart.umd.js.

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

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

like image 158
dosfr332 Avatar answered Nov 21 '25 07:11

dosfr332