[RC0] How to import "xml2js"


#1

After RC0 upgrade the xml2js import failed :

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

the code :

import xml2js from "xml2js";

@Injectable()
export class XMLDataService {
  constructor() {
    console.log(xml2js)
  }
}

ionic serve log me that no names are provided for module stream, string_decoder, events, timer…

[10:22:42]  bundle dev started ...

[10:22:48]  rollup: Treating 'stream' as external dependency
preferring built-in module 'string_decoder' over local alternative at '[...]\node_modules\string_decoder\index.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
[10:22:48]  rollup: Treating 'string_decoder' as external dependency
preferring built-in module 'string_decoder' over local alternative at '[...]\node_modules\string_decoder\index.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
[10:22:48]  rollup: Treating 'events' as external dependency
[10:22:49]  rollup: Treating 'timers' as external dependency

[10:22:53]  rollup: No name was provided for external module 'stream' in options.globals - guessing 'stream'
[10:22:53]  rollup: No name was provided for external module 'string_decoder' in options.globals - guessing 'string_decoder'
[10:22:53]  rollup: No name was provided for external module 'events' in options.globals - guessing 'events'
[10:22:53]  rollup: No name was provided for external module 'timers' in options.globals - guessing 'timers'

[10:22:55]  bundle dev finished in 12.79 s

and so on chrome :

What am I doing wrong? :unamused:


How convert XML input file to json
#3

I still don’t know how to fix it…

After all, I uninstall xml2js and install x2js
if someone needs the procedure :

my ionic conf :

Cordova CLI: 6.2.0
Ionic Framework Version: 2.0.0-rc.0
Ionic CLI Version: 2.1.0
Ionic App Lib Version: 2.1.0-beta.1
OS: Windows 7 SP1
Node Version: v5.6.0

npm install :

npm install x2js --save
npm install @types/x2js --save-dev --save-exact

versions that I use :

"x2js": "3.0.1"
"@types/x2js": "0.0.27"

add declaration to the types index :

to add at the end of _./node_modules/@types/x2js/index.d.ts

declare module 'x2js' {
    export = X2JS;
}

code sample :

import X2JS from 'x2js';

let xml =  "MY XML STRING";
let parser : any = new X2JS();
let json = parser.xml2js(xml);