Unable to initialize Editor
Unable to initialize Editor
haarhoff
Posts: 4Questions: 1Answers: 0
Error messages shown:
Uncaught TypeError: (init_dataTables_editor(...) , __toCommonJS(...)) is not a function
at new module.exports (editor.bootstrap5.js:25:5)
at campaign.js:13:16
**Description of problem**:
Hi, I'm trying to evaluate DataTable Editor. The JS is served by Vite. This is the module file:
import jQuery from "jquery";
import DataTable from 'datatables.net-bs5';
import Editor from 'datatables.net-editor-bs5';
const editor = new Editor(window, jQuery);
const table = new DataTable('#campaign-table',{
dom: 'Bfrtip',
paging: true,
pageLength: 10,
buttons: [
{ extend: 'create', editor: editor },
{ extend: 'edit', editor: editor },
{ extend: 'remove', editor: editor }
],
});
Any hint on what might go wrong here would be greatly appreciated.
Answers
Vite uses ES modules if they are available, which they are with Editor. Our ES modules have a slightly different signature to the CommonJS modules - the upshot of which is that you should drop:
Instead, just use the
Editor
module in a similar way to how you use theDataTable
one - i.e. initialise the Editor on that module:That should do the job for you. If it doesn't (the
__toCommonJS
is giving me a bit of pause) can you show me your package.json file and any Vite configuration you have. Or even better would be a link to a repo showing the issue.Thanks,
Allan
Thanks, its unfortunately still displaying the same error message.
Its an ASP.NET Core project, but I'd be happy to extract a minimal example showing the error if that works for you.
Here is my vite configuration:
Nothing spectacular
The package.json is similarly simple:
Hi Allan,
Thanks for your reply. I'm happy to extract a minimal example, it would be an ASP.NET Core project though, if thats allright with you.
Here is my vite config:
And my package.json:
Hi,
Apologies for the delay in getting back to you about this one!
Try the following:
The difference is that I've imported from
datatables.net-editor
first. That shouldn't be required - thedatatables.net-editor-bs5
package should be enough, but there is something going wrong with the CommonJS / ES module compatibility there that I will need to investigate further.Allan
I've realised what was going wrong. The Editor styling packages on npm.com did not have a
module
attribute in their package.json file. The result was that it was falling back to CommonJS mode for those files, but using ES modules for the main Editor files. The mix of the two caused the error.I've published updates to those packages now to address this.
We also have our own NPM repository which didn't have this issue and can simplify the installation of the Editor modules (since you don't need to run the zip install script). Documentation on how to use it is available here.
Regards,
Allan