简体   繁体   中英

xml2js in Angular 6: Can't resolve 'stream' and 'timers'

I would like to parse xml data, retrieved from the server. Unfortunately HttpClient does not support xml, only json, therefore I installed the package xml2js :

npm install xml2js --save
npm install @types/xml2js --save-dev

Then I try to use it like this:

import {Parser} from 'xml2js';

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

I get these two errors if I run it:

WARNING in ./node_modules/xml2js/node_modules/sax/lib/sax.js
Module not found: Error: Can't resolve 'stream' in 'C:\projects\app\node_modules\xml2js\node_modules\sax\lib'

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

I haven't found any solutions to this problem, maybe it is an Angular6 issue only. Is there any way, to parse xml in Angular6?

You'll have to install those dependencies. It's unfortunately not well documented.

npm install --save stream timers

For Angular to recognise xml2js, the following steps are required:

  1. Add the timers-browserify node module using "npm install timers-browserify"
  2. Add the stream-browserify node module using "npm install stream-browserify"
  3. Add the following path mapping in your tsconfig.json:
    "compilerOptions": {
      "paths": {
        "timers": [
          "node_modules/timers-browserify"
        ],
        "stream": [
          "node_modules/stream-browserify"
        ],
      }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM