Created my own custom export server from here: https://github.com/highcharts/node-export-server, at the time time of writting v 4.0.2. Installed puppeteer, chrome, a bunch of required libraries, pm2 and node v20.15.1.
After this, while using the CLI, enabled the export server with the command highcharts-export-server --enableServer 1 --port 8000 --noDownload 0
and everything works as expected.
As per the documentation from the above, I'm trying to keep the process alive using pm2. After running this command: pm2 start highcharts-export-server -- --enableServer 1 --port 8000 --noDownload 0
the process starts and when I check it with pm2 list
it has the status errored
.
The error below:
Error [ERR_REQUIRE_ESM]: require() of ES Module /var/www/.nvm/versions/node/v20.15.1/lib/node_modules/highcharts-export-server/bin/cli.js not supported.
Instead change the require of cli.js in null to a dynamic import() which is available in all CommonJS modules. at Object. (/var/www/.nvm/versions/node/v20.15.1/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23) { code: 'ERR_REQUIRE_ESM' }
From googling I've found out that certain libraries are only compatible with ESM modules and thus wants you to load it with import
and not require()
, searched the code and couldn't find any instance of require()
I am not sure how to proceed further
There is no require()
in the highcharts-export-server
code there, but pm2
uses require()
when you start the highcharts-export-server
without specifying how to handle ES modules; pm2
tries to load the script using require()
which leads to this error.
You could try out one of the following solutions:
Try to run pm2
with an additional node flag: pm2 start "highcharts-export-server/bin/cli.js" --node-args="--experimental-modules" -- --enableServer 1 --port 8000 --noDownload 0
Rename the file to .mjs
: pm2 start cli.mjs -- --enableServer 1 --port 8000 --noDownload 0
Use Export Server (/dist
) as a node module in your application (either CommonJS or ESM, the /dist
directory is built by Rollup with the npm run build
command but it's also available on GH):
// Import the Highcharts Export Server module
const exporter = require('highcharts-export-server');
// Export options correspond to the available CLI/HTTP arguments described above
const options = {
export: {
type: 'png',
options: {
title: {
text: 'My Chart'
},
xAxis: {
categories: ["Jan", "Feb", "Mar", "Apr"]
},
series: [
{
type: 'line',
data: [1, 3, 2, 4]
},
{
type: 'line',
data: [5, 3, 4, 2]
}
]
}
}
};
// Initialize export settings with your chart's config
const exportSettings = exporter.setOptions(options);
// Must initialize exporting before being able to export charts
await exporter.initExport(exportSettings);
// Perform an export
await exporter.startExport(exportSettings, async (error, info) => {
// The export result is now in info
// It will be base64 encoded (info.data)
// Kill the pool when we are done with it
await exporter.killPool();
});