Closed banujan6 closed 9 months ago
That the cjs version is still available in dist is a mistake and it is an outdated version. The move to esm was done to reduce the burden of maintenance of this package.
If you need types of a dynamically imported module, you can use this syntax:
type MyType = import('svgdom').Node
Also consider using bun which can reolve esm and cjs imports without using any async import(). In that case you dont even need to transpile the file
I tried async load too
const { createSVGWindow } = await import( 'svgdom' );
global.window = createSVGWindow();
global.document = global.window.document;
registerWindow(window, document);
It generated the following code:
const { createSVGWindow } = yield Promise.resolve().then(() => __importStar(require('svgdom'))); // line 51
global.window = createSVGWindow();
global.document = global.window.document;
(0, svg_js_1.registerWindow)(window, document);
Still, this error occurs when loading the module.
Error [ERR_REQUIRE_ESM]: require() of ES Module /node_modules/svgdom/main-module.js from /dist/services/svg-split.svc.js not supported. Instead change the require of main-module.js in /dist/services/svg-split.svc.js to a dynamic import() which is available in all CommonJS modules. at /dist/services/svg-split.svc.js:51:89 { code: 'ERR_REQUIRE_ESM' }
So it converts the import to a require call? try to ts-ignore that line
Well, I fixed it. Since it is a back-end service, There is no point of building it. I could directly run it with ts-node
instead of building. Thank you for your support.
that works! If you use ts-node anyway, you might wanna try tsx or bun as alternatives
I have been using
svgdom: 0.1.14
in mytypescript
project which eventually gets compiled toCommonJS
. After updatingsvgdom
to0.1.19
, I lost the support toCommonJS
and it throws the following error after a successful build.NOTE: Due to some reasons I can't switch to ESM. Also since I have used
svgdom
inexport abstract class
as a type reference, I cannot even usedynamic import()
.Then noticed that the
package.json
has been set to exportesm
version for both theexport
andrequire
even thocjs
is available indist/
directory.I could solve the issue by updating the
package.json
as follows,Is there any special reason to serve
esm
for bothrequire
andimport
? It would be great if we get both support.