Closed joselee closed 11 years ago
What is the test.xml that you are parsing? Have you tried running the unittests, if so, did they work?
Note that the problem is while instantiating Parser. In my case:
var xml2jsparser = require('xml2js').Parser({"explicitArray":true,"ignoreAttrs":true});
is the line that causes the problem, well before I try to process an XML string.
You forgot to use the new
keyword,
var xml2js = require('xml2js')
var xml2jsparser = new xml2js.Parser({"explicitArray":true,"ignoreAttrs":true});
When you add this keyword you can create instances just fine.
Yep I can confirm that it was indeed the lack of new.
Thanks.
@Leonidas-from-XIV
Would you be interested in eliminating any further issues related to the lack of new
?
That could be accomplished by turning xml2js.Parser()
into some John Resig's self-calling constructor (that autoruns new
before itself whenever new
is missing).
Such a change would be a pretty one-liner, like if ( !(this instanceof exports.Parser) ) return new exports.Parser(opts)
in the first line of your Parser
's constructor, if it were in JavaScript.
(However, it's in CoffeeScript — and I don't know CoffeeScript enough to make a reliable pull request, even for a one-liner.)
@Mithgol I agree this is a good idea, can you open a ticket to keep track of this feature? I'll try to add that ASAP, should be very similar in CoffeeScript as well.
Okay. Opened #89.
Thanks. Then this issue can be considered closed for good.
I've also noticed that I somehow ended making the hyperlink titled “John Resig's self-calling constructor” pointing to http://habrahabr.ru/post/135027/ (a detailed article, but in Russian and thus not internationally helpful) instead of the intended http://ejohn.org/blog/simple-class-instantiation/ (the original Resig's blog entry) yesterday.
(That's fixed now.)
Note to others that may happen upon this issue, as I did. I had the same error with a modern xml2js (so it wasn't the same problem). The problem was building in an environment (vite with vite-plugin-electron-renderer) that was choking on the cjs nature of this older library. The fix was to tell that environment leave xml2js as an "external package".
Hi @hatton, How do we let Vite know xml2js as external package. I'm facing error: TypeError: this.removeAllListeners is not a function at Parser.reset (parser.js:111:12)
See https://vitejs.dev/config/dep-optimization-options.html. In my case, I have
optimizeDeps: {
include: ["xml2js", "glob", "fs-extra", "graceful-fs"]
}
Thanks, @hatton .. However, the issue still exists..
Getting the same issue, after adding "optimizeDeps" as recommended by @hatton. Any other clues here? Thanks
in my case, installing events solved the issue: npm i events
Good afternoon, same error, seeking for help please !
Thank you in advance !
Empty NUXT 3 project, npm i xml2js + timers
package.json: { "name": "nuxt-app", "private": true, "scripts": { "build": "nuxt build", "dev": "nuxt dev", "generate": "nuxt generate", "preview": "nuxt preview", "postinstall": "nuxt prepare" }, "devDependencies": { "@nuxt/devtools": "latest", "@types/node": "^18.17.3", "nuxt": "^3.6.5" }, "dependencies": { "timers": "^0.1.1", "xml2js": "^0.6.2" } }
nuxt.config.ts: // https://nuxt.com/docs/api/configuration/nuxt-config export default defineNuxtConfig({ devtools: { enabled: true }, vite:{ optimizeDeps: { include: ["xml2js"] } } })
app.vue:
sax.js:222 Uncaught TypeError: Cannot read properties of undefined (reading 'prototype')
at sax.js:222:46
at node_modules/sax/lib/sax.js (sax.js:1565:1)
at require (chunk-RSJERJUL.js?v=b6cf498b:3:50)
at Object.
@Pic212 is helpful
<script setup> import { parseString } from 'xml2js'; </script>
+1 on @Pic212 's latest answer above:
npm i events
resolved the error.
same as @jhfoo
npm i events
.
and then rerun your project. worked for me.
thanks @josephosan @jhfoo @Pic212 . Installing events resolved the error:
npm i events
Tried running a simple test. Here's my server.js:
var fs = require('fs'); var xml2js = require('xml2js');
var parser = xml2js.Parser();
fs.readFile(__dirname + '/test.xml', function(err, data) { parser.parseString(data, function(err, result){ console.dir(result); console.log("done"); }); });
Console throws an error: Object #
Happens on /node_modules/xml2js/lib/xml2js.js:103 trying to do this.removeAllListeners()