Issue
My angular project has started giving error during build post addition of libxmljs package. But strange thing is error is pointing to folder - \node_modules\bindings instead of \node_modules\libxmljs.
Only code change -
"libxmljs": "^0.19.7"
in package.json
and
let xsdDoc = libxmljs.parseXml(xsdFile);
in myproject.component.ts
My environment -
Node v18.0.0
NPM 8.6.0
No angular CLI. Its maven project.
Error log -
...
[INFO] ./node_modules/bindings/bindings.js:4:9-22 - Error: Module not found: Error: Can't resolve 'fs' in 'D:\my-frontend\node_modules\bindings'
[INFO]
[INFO] ./node_modules/bindings/bindings.js:5:11-26 - Error: Module not found: Error: Can't resolve 'path' in 'D:\my-frontend\node_modules\bindings'
[INFO]
[INFO] BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
[INFO] This is no longer the case. Verify if you need this module and configure a polyfill for it.
[INFO]
[INFO] If you want to include a polyfill, you need to:
[INFO] - add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
[INFO] - install 'path-browserify'
[INFO] If you don't want to include a polyfill, you can use an empty module like this:
[INFO] resolve.fallback: { "path": false }
[INFO]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 07:51 min
...
Troubleshoot -
- Tried
"browser": { "fs": false, "path":false }
with no luck. - Tried webpack.config.js but same issue.
Is this a bug in libxmljs and should I find other way to validate xmls ?
Solution
It seems node_modules/bindings
specific bug.
I resolved issue by switching to java using javax.xml
.
Sample code -
@PostMapping("/xml/validate")
public ResourceEntity<Boolean> validateXmlWithXSD(HttpServletRequest request, @RequestHeader(value = "Authorization", required = true) String token, int xsdVersion, @RequestBody ProjectXml processIn) {
//step- initializations
...
try {
//step- session validation
...
//step- xsd access from webapp
String xsdPath = "/assets/xsds/ProjectXsd." + xsdVersion + ".xsd";
InputStream xsdStream = request.getSession().getServletContext().getResourceAsStream(xsdPath);
if(xsdStream==null)
throw new NullPointerException("Null xsd stream.");
//step- xml validation against xsd
Source source = new StreamSource(xsdStream);
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(source);
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new ByteArrayInputStream(processIn.getProcessXML())));
isValidated = true;
} catch (SAXException | IOException | NullPointerException e) {
...
}
//step- return result
}
Answered By - PriyankaW
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.