Search code examples
xmlnode.jsxsltxml-parsingrequire

Require is not defined xml2js


I would like to use the library xml2js. I install it on the project folder with the npm.

var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js!</root>";
parseString.(xml, function (err, result) {
    console.dir(result);
});

I have an error when i execute node myfile.js:

d:\Profiles\user\ProjectIDE\Aproject\parseXML.js:3
parseString.(xml, function (err, result) {
            ^

SyntaxError: Unexpected token (
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

Solution

  • Syntax Error:

    Instead of this:

    parseString.(xml, function (err, result) {
        console.dir(result);
    });
    

    Try this:

    parseString(xml, function (err, result) {
        console.dir(result);
    });