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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With