You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I created an Angular CLI (1.6.5) project and after that I install fast-xml-parser (npm install fast-xml-parser --save (2.9.3) and run the app with ng serve. Until here everything is OK.
Now I try to use it in the way you described
In a default component I try to add the require
`
import { Injectable } from '@angular/core';
var fastXmlParser = require('fast-xml-parser');
`
ng serve detect the change and shows the error
`webpack: Compiled successfully.
webpack: Compiling...
90% chunk assets processingERROR in src/app/i-hub/imp-exp.service.ts(4,21): error TS2304: Cannot find name 'require'.
Date: 2018-01-30T10:22:19.516Z • Hash: d91f217da47d8c3a4021 • Time: 2927ms
3 unchanged chunks
chunk {main} main.bundle.js (main) 59.3 kB [initial] [rendered]
chunk {vendor} vendor.bundle.js (vendor) 14.4 MB [initial] [rendered]
webpack: Compiled successfully.`
What should I do with the unknown require error, how could I solve this
Could you please provide me an exact syntax? Everything I try did not work
some errors
scripts.bundle.js:1 Uncaught ReferenceError: require is not defined
at scripts.bundle.js:1
(anonymous) @ scripts.bundle.js:1
core.js:3675 Angular is running in the development mode. Call enableProdMode() to enable the production mode.
i-hub-header.component.ts:21 onDashboardClicked(..) executed.
core.js:1440 ERROR TypeError: Cannot read property 'parse' of undefined
at FileReader.reader.onloadend (imp-exp.service.ts:33)
at ZoneDelegate.invoke (zone.js:388)
at Object.onInvoke (core.js:4733)
at ZoneDelegate.invoke (zone.js:387)
at Zone.runGuarded (zone.js:151)
at FileReader.eval (zone.js:129)
.....
My source code
`
import { Injectable } from '@angular/core';
//var fastXmlParser = require('fast-xml-parser');
import parser from 'fast-xml-parser';
//import { parser } from 'fast-xml-parser';
With Angular 13 I was getting Type 'XMLParser' has no call signatures. I tweaked it based on your example and looks like I was missing the parser.parse. Thanks.
Activity
amitguptagwl commentedon Jan 30, 2018
fast-xml-parseror when you use it in your code?requireto importfast-xml-parser?I believe the problem is more related to syntax not the library. So please change it to
import parser from fast-xml-parserand then try.jstolz commentedon Jan 30, 2018
Could you please provide me an exact syntax? Everything I try did not work
My source code
`
import { Injectable } from '@angular/core';
//var fastXmlParser = require('fast-xml-parser');
import parser from 'fast-xml-parser';
//import { parser } from 'fast-xml-parser';
@Injectable()
export class ImpExpService {
private files: any;
private fileContent: any;
constructor() {
}
setLoadedFiles(loadedFiles: any) {
}
}
`
amitguptagwl commentedon Jan 30, 2018
I believe the issue is related to ES6 implementation. Check https://medium.com/the-node-js-collection/an-update-on-es6-modules-in-node-js-42c958b890c
Have you tried to import any other 3rd party library?
jstolz commentedon Feb 2, 2018
Hi,
I found a solution, just add the line "declare var require: any;" before the require.
Regards Jürgen
savvasmarkstatkus commentedon Mar 22, 2022
I had better luck just using v3.19.0 vs 4 in Angular. Didn't want to spend anymore time dealing with TS errors.
"fast-xml-parser": "^3.19.0",
import { parse } from "fast-xml-parser"; // Couldn't get v4 to work, v3 seemed to.
this.result = parse(xmlStr, options);
amitguptagwl commentedon Mar 23, 2022
@savvasmarkstatkus APIs are changed in v4.
Eg
savvasmarkstatkus commentedon Mar 23, 2022
With Angular 13 I was getting Type 'XMLParser' has no call signatures. I tweaked it based on your example and looks like I was missing the
parser.parse. Thanks.