Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.89k stars 606 forks source link

calling removeAllListeners() method which doesn't exit #88

Closed joselee closed 11 years ago

joselee commented 11 years ago

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 # has no method 'removeAllListeners'

Happens on /node_modules/xml2js/lib/xml2js.js:103 trying to do this.removeAllListeners()

Leonidas-from-XIV commented 11 years ago

What is the test.xml that you are parsing? Have you tried running the unittests, if so, did they work?

albertoa commented 11 years ago

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.

Leonidas-from-XIV commented 11 years ago

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.

albertoa commented 11 years ago

Yep I can confirm that it was indeed the lack of new.

Thanks.

Mithgol commented 11 years ago

@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.)

Leonidas-from-XIV commented 11 years ago

@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.

Mithgol commented 11 years ago

Okay. Opened #89.

Leonidas-from-XIV commented 11 years ago

Thanks. Then this issue can be considered closed for good.

Mithgol commented 11 years ago

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.)

hatton commented 1 year ago

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".

VaneeOfficialNet commented 1 year ago

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)

hatton commented 1 year ago

See https://vitejs.dev/config/dep-optimization-options.html. In my case, I have

      optimizeDeps: {
        include: ["xml2js", "glob", "fs-extra", "graceful-fs"]
      }
VaneeOfficialNet commented 1 year ago

Thanks, @hatton .. However, the issue still exists..

psantos9 commented 1 year ago

Getting the same issue, after adding "optimizeDeps" as recommended by @hatton. Any other clues here? Thanks

Screenshot 2023-05-16 at 18 22 09

Pic212 commented 1 year ago

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. (parser.js:9:9) at node_modules/xml2js/lib/parser.js (parser.js:395:4) at __require (chunk-RSJERJUL.js?v=b6cf498b:3:50) at Object. (xml2js.js:12:12) at node_modules/xml2js/lib/xml2js.js (xml2js.js:39:4) at require (chunk-RSJERJUL.js?v=b6cf498b:3:50) at xml2js.js:39:13

eimlfang commented 1 year ago

@Pic212 is helpful <script setup> import { parseString } from 'xml2js'; </script>

jhfoo commented 1 year ago

+1 on @Pic212 's latest answer above:

npm i events resolved the error.

josephosan commented 1 year ago

same as @jhfoo npm i events. and then rerun your project. worked for me.

willymateo commented 11 months ago

thanks @josephosan @jhfoo @Pic212 . Installing events resolved the error: npm i events