node-xml2js icon indicating copy to clipboard operation
node-xml2js copied to clipboard

Requiring unknown module 'timers'

Open RezaRahmati opened this issue 8 years ago • 19 comments

It seems that project is dependents on 'timers', but it's not added in the package.json

RezaRahmati avatar May 16 '16 15:05 RezaRahmati

That module is part of Nodejs.

Leonidas-from-XIV avatar May 16 '16 15:05 Leonidas-from-XIV

Thanks for fast response, I use it in React-Native project, so can you give me a clue why I get this error?

RezaRahmati avatar May 16 '16 15:05 RezaRahmati

FYI, I added
"timers": "^0.1.1", to dependencies in package.json and npm install again, it works fine

RezaRahmati avatar May 16 '16 17:05 RezaRahmati

That's odd, that module does not seem to have any setImmediate function, which is the only one required by xml2js.

Leonidas-from-XIV avatar May 16 '16 17:05 Leonidas-from-XIV

It's timers-browserify

davcs86 avatar Jun 29 '16 16:06 davcs86

Hi,

Still facing this issue:

ERROR in ./node_modules/xml2js/lib/parser.js
Module not found: Error: Can't resolve 'timers' in 'somepath\node_modules\xml2js\lib'

and

WARNING in ./node_modules/sax/lib/sax.js
Module not found: Error: Can't resolve 'stream' in 'somepath\node_modules\sax\lib'

Adding both dependencies solves the issues but I don't think this is the way it should be handled.

mboughaba avatar May 26 '18 13:05 mboughaba

Are you running it in a non-node environment or something?

Leonidas-from-XIV avatar May 26 '18 14:05 Leonidas-from-XIV

I am running it in an Angular project on windows. Both dependencies were resolved before by some other dependencies in Angular itself. So they were found under ./node_modules. Now that I upgraded Angular I started getting those errors.

mboughaba avatar May 26 '18 14:05 mboughaba

Then you probably do need these timers emulation dependencies if you're running the code in the browser.

Leonidas-from-XIV avatar May 26 '18 14:05 Leonidas-from-XIV

Great, Thank you for the confirmation. Will it help if we add those as peer dependencies? this way one would get a warning about it.

mboughaba avatar May 26 '18 14:05 mboughaba

Hmm, maybe I am misunderstanding how that would work, but to me it doesn't look like setting them as peer dependencies will solve the issue.

Another solution would be to publish an angular-xml2js package which depends on node-xml2js and one of the timers modules.

Leonidas-from-XIV avatar May 26 '18 14:05 Leonidas-from-XIV

~~Yes, that is a better solution. However angular-xml2js will need to be maintained separately.~~ @Leonidas-from-XIV I created browser-xml2js as suggested. Now I need to publish an npm package and keep it updated with this repository 🥂

Cheers

mboughaba avatar May 26 '18 14:05 mboughaba

Since angular explicitly disables default node modules, the build will break on them.

krojew avatar Mar 07 '19 10:03 krojew

@krojew what do you mean? I use this and browser-xml2js in angular and build is fine

mboughaba avatar Mar 07 '19 10:03 mboughaba

If you don't use the browser version, it tries to use node modules which angular disabled during building. Also, browser-xml2js needs setImmediate at runtime function which doesn't exist and results in an error.

krojew avatar Mar 07 '19 10:03 krojew

For Angular in-browser usage, add path mapping in your tsconfig.json:

  "compilerOptions": {
    "paths": {
      "timers": [
        "node_modules/timers-browserify"
      ],
      "stream": [
        "node_modules/stream-browserify"
      ],
    }
  }

A similar issue was discussed in angular-cli: https://github.com/angular/angular-cli/issues/10694#issuecomment-428520833

For browser-specific shims or mocks, see https://github.com/webpack/node-libs-browser or https://github.com/browserify/browserify#compatibility .

Bi11 avatar Apr 03 '19 15:04 Bi11

I think "timers" should be part of the dependencies. On environments that don't need it it will do nothing, and on those that miss it it will fix the issue.

laurent22 avatar Nov 04 '20 17:11 laurent22

seems there's an issue with xml2js in react native with expo keeps throwing the error and on installing module "timers" results in an app not initialized error just found another package x2js which is more recent, works without any issue and best case is written in typescript

Ckm54 avatar Mar 17 '23 20:03 Ckm54

seems there's an issue with xml2js in react native with expo keeps throwing the error and on installing module "timers" results in an app not initialized error just found another package x2js which is more recent, works without any issue and best case is written in typescript

THANK YOU, this fixed the problem for me! Had to replace all references to xml2js in xml.js with x2js and azure/storage-blob no longer breaks my project!

adam-b-jones avatar Mar 30 '23 14:03 adam-b-jones