-1

New to Angular, I have been experimenting with the installed node modules and their invocation in a data.service.ts file. All of the code below works just fine!

var isWindows = require('is-windows');
console.log("Is this windows? " + isWindows());

var builder = require('xmlbuilder');
var xml = builder.create('root')
 .ele('xmlbuilder')
 .ele('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git')
.end({ pretty: true});
console.log(xml);

var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js!</root>"
parseString(xml, function (err, result) {
console.log(result);
});

The function I really need is the more complex xml2json function. This has a directory amongst my node_modules. When I add this line to the code (as per the documentation for this function):

       var parser = require('xml2json');

ALL of the require statements are flagged as errors by the compiler!

ERROR in src/app/data.service.ts(45,20): error TS2304: Cannot find name 'require'. src/app/data.service.ts(48,18): error TS2304: Cannot find name 'require'. src/app/data.service.ts(55,22): error TS2304: Cannot find name 'require'.

If I try to declare this function via an import statement, that works UNTIL I add code to try to invoke the function -- at which time I get the same errors.

I notice the directory structure of the xml2json node is more complex than many others having a /bin, /lib and even a /node-modules (1 entry hoek) and perhaps my require statement is too simple.

These error messages seem to be fairly common in Angular, but none of the questions posted seem to be my exact problem. It seems that the require statement fails in some way when trying to acquire the xml2json resource, and that immediately impacts the other 'require' statement.

In my package.json file dependencies this node is listed as:

                "xml2json": "^0.11.2",

Any guidance will be appreciated!

Harry Whitehouse
  • 41
  • 1
  • 1
  • 6
  • https://stackoverflow.com/questions/42891332/how-to-import-xml2js-in-an-angular2-app-using-angular-cli-with-webpack/42891852#42891852 similar issue with a "fix" for Angular 6.0. But still not working in Angular 6.1. – Harry Whitehouse Sep 15 '18 at 21:56

1 Answers1

0

Unfortunately as Angular is a front-end web framework, and packages that work exclusively in Node.js won't work in Angular.

The only way to get them to work would be to have them on your web server, and communicate with your Angular application through web requests.

Bennett Hardwick
  • 1,359
  • 9
  • 16
  • Thank you Bennett. How can one tell if a node.js module will work with the front end framework (other than the "require' error mentioned above?) – Harry Whitehouse Sep 15 '18 at 15:58
  • https://stackoverflow.com/questions/42891332/how-to-import-xml2js-in-an-angular2-app-using-angular-cli-with-webpack/42891852#42891852 suggests that you can use xml2js with Webpack. The suggestions there suggest adding two more components for it to work with A 6.0. I tried those but I'm still not working in A 6.1. – Harry Whitehouse Sep 15 '18 at 21:55