Install the libxml2-wasm
npm package using your preferred method,
such as:
npm install libxml2-wasm
Now, let’s import the libxml2-wasm module. Since it’s an ES module, the import process differs between ES modules and CommonJS modules.
Import it directly.
import { XmlDocument } from 'libxml2-wasm';
const doc = XmlDocument.fromString('<note><to>Tove</to></note>');
doc.dispose();
Dynamic import is needed:
import('libxml2-wasm').then(({ XmlDocument }) => {
const doc = XmlDocument.fromString('<note><to>Tove</to></note>');
doc.dispose();
});
Important Note:
Remember to call the dispose()
method on the XmlDocument instance to prevent memory leaks.
For more detailed information on memory management, refer to Memory Management.
Troubleshooting:
If the target environment version is set too low,
the transpiler (e.g., TypeScript, Babel, etc.) may convert the import
statement to a function call to require()
.
This can lead to runtime errors.