Skip to content

TypeError when using CommonJS: XMLParser is not a constructor #719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 of 7 tasks
tbouffard opened this issue Feb 20, 2025 · 4 comments
Closed
6 of 7 tasks

TypeError when using CommonJS: XMLParser is not a constructor #719

tbouffard opened this issue Feb 20, 2025 · 4 comments

Comments

@tbouffard
Copy link
Contributor

  • Are you running the latest version? 5.0.2
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration? Take the example from the readme
  • Did you try online tool?
  • Have you checked the docs for helpful APIs and examples?

Description

When using CommonJS, my application is unable to create an instance of XMLParser or XMLBuilder
I get errors like TypeError: XMLParser is not a constructor when running with node or when running tests with Jest TypeError: fast_xml_parser_1.XMLParser is not a constructor
I have the same kind of error when trying to use XMLBuilder

For example (run on Ubuntu 22.04, node v20.18.3)

$ node parse.cjs
/dev/bump_fxp_v5_poc/repro_commonjs_pb/parse.cjs:15
const parser = new XMLParser();
               ^

TypeError: XMLParser is not a constructor
    at Object.<anonymous> (/dev/bump_fxp_v5_poc/repro_commonjs_pb/parse.cjs:15:16)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
    at Module.load (node:internal/modules/cjs/loader:1288:32)
    at Module._load (node:internal/modules/cjs/loader:1104:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:173:12)
    at node:internal/main/run_main_module:28:49

Input

Here are scripts to reproduce the problem. I run them in a fresh project with a single dependency to fast-xml-parser

Code

CommonJS: not working

const { XMLParser, XMLBuilder } = require('fast-xml-parser');

const xml = `<?xml version="1.0"?>
<any_name>
    <person>
        <phone>+122233344550</phone>
        <name>Jack</name>
    </person>
    <person>
        <phone>+122233344553</phone>
        <name>Boris</name>
    </person>
</any_name>`;

const parser = new XMLParser();
const parsedObject = parser.parse(xml);
console.log('parsedObject', JSON.stringify(parsedObject, null, 2));

const builder = new XMLBuilder();
const xmlContent = builder.build({ note: 'hello' });
console.log('xmlContent', xmlContent);

ESM: working

import { XMLParser, XMLBuilder } from 'fast-xml-parser';

const xml = `<?xml version="1.0"?>
<any_name>
    <person>
        <phone>+122233344550</phone>
        <name>Jack</name>
    </person>
    <person>
        <phone>+122233344553</phone>
        <name>Boris</name>
    </person>
</any_name>`;

const parser = new XMLParser();
const parsedObject = parser.parse(xml);
console.log('parsedObject', JSON.stringify(parsedObject, null, 2));

const builder = new XMLBuilder();
const xmlContent = builder.build({ note: 'hello' });
console.log('xmlContent', xmlContent);

Output

CommonJS: see description at the top of this issue

expected data

Same as ESM:

$ node parse.mjs 
parsedObject {
  "?xml": "",
  "any_name": {
    "person": [
      {
        "phone": 122233344550,
        "name": "Jack"
      },
      {
        "phone": 122233344553,
        "name": "Boris"
      }
    ]
  }
}
xmlContent <note>hello</note>

Would you like to work on this issue?

  • Yes
  • No, but I can help by testing PR
@JakobMe
Copy link

JakobMe commented Feb 20, 2025

I have the same issue using v5 with Jest. The culprit seems to be this line in lib/fxp.cjs:

/******/  module.exports.fxp = __webpack_exports__;

It should actually be module.exports = __webpack_exports__; to work properly, otherwise you get

{
  fxp: Object [Module] {
    XMLBuilder: [Getter],
    XMLParser: [Getter],
    XMLValidator: [Getter]
  }
}

by importing it.

@amitguptagwl
Copy link
Member

issue is resolved will be publishing the changes soon

@amitguptagwl
Copy link
Member

done, please reopen if you still find the issue.

@tbouffard
Copy link
Contributor Author

tbouffard commented Feb 20, 2025

ℹ I have tested 5.0.4 with the CommonJS script provided in this issue: no more error 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants