在电子应用程序中使用 libxml-xsd 失败并出现绑定错误

Posted

技术标签:

【中文标题】在电子应用程序中使用 libxml-xsd 失败并出现绑定错误【英文标题】:Using libxml-xsd in an electron app fails with bindings error 【发布时间】:2017-12-23 05:32:58 【问题描述】:

我想在电子应用程序中根据其架构验证 XML 文件。

我有一个 angular2 服务,它在包含我的 libxml-xsd 要求语句之前工作。一旦我将它添加到我的服务文件的顶部:

const xsd = require('libxml-xsd');

我运行 npm run build 执行此操作:

webpack --progress --profile --colors --display-error-details --display-cached

我在输出中看到以下新警告:

WARNING in ./~/bindings/bindings.js
Critical dependencies:
76:22-40 the request of a dependency is an expression
76:43-53 the request of a dependency is an expression
 @ ./~/bindings/bindings.js 76:22-40 76:43-53

WARNING in ./~/bindings/README.md
Module parse failed: /Users/myusername/Documents/Personal/MyApp/node_modules/bindings/README.md Unexpected token (2:3)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:3)
    at Parser.pp$4.raise (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:2221:15)
    at Parser.pp.unexpected (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:603:10)
    at Parser.pp$3.parseExprAtom (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1822:12)
    at Parser.pp$3.parseExprSubscripts (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1715:21)
    at Parser.pp$3.parseMaybeUnary (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1692:19)
    at Parser.pp$3.parseExprOp (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1656:43)
    at Parser.pp$3.parseExprOp (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1658:21)
    at Parser.pp$3.parseExprOps (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1639:17)
    at Parser.pp$3.parseMaybeConditional (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1620:21)
    at Parser.pp$3.parseMaybeAssign (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1597:21)
    at Parser.pp$3.parseExpression (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1573:21)
    at Parser.pp$1.parseStatement (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:727:47)
    at Parser.pp$1.parseTopLevel (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:638:25)
    at Parser.parse (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:516:17)
    at Object.parse (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:3098:39)
    at Parser.parse (/Users/myusername/Documents/Personal/MyApp/node_modules/webpack/lib/Parser.js:902:15)
    at DependenciesBlock.<anonymous> (/Users/myusername/Documents/Personal/MyApp/node_modules/webpack/lib/NormalModule.js:104:16)
    at DependenciesBlock.onModuleBuild (/Users/myusername/Documents/Personal/MyApp/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
    at nextLoader (/Users/myusername/Documents/Personal/MyApp/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
    at /Users/myusername/Documents/Personal/MyApp/node_modules/webpack-core/lib/NormalModuleMixin.js:259:5
    at Storage.finished (/Users/myusername/Documents/Personal/MyApp/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:38:16)
    at /Users/myusername/Documents/Personal/MyApp/node_modules/graceful-fs/graceful-fs.js:78:16
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:446:3)
 @ ./~/bindings ^\.\/.*$

然后我通过执行:electron main.js 来运行我的应用程序,然后在浏览器控制台中抛出此错误:

Uncaught Error: Could not locate the bindings file. Tried:
 → /Users/myusername/Documents/Personal/MyApp/build/xmljs.node
 → /Users/myusername/Documents/Personal/MyApp/build/Debug/xmljs.node
 → /Users/myusername/Documents/Personal/MyApp/build/Release/xmljs.node
 → /Users/myusername/Documents/Personal/MyApp/out/Debug/xmljs.node
 → /Users/myusername/Documents/Personal/MyApp/Debug/xmljs.node
 → /Users/myusername/Documents/Personal/MyApp/out/Release/xmljs.node
 → /Users/myusername/Documents/Personal/MyApp/Release/xmljs.node
 → /Users/myusername/Documents/Personal/MyApp/build/default/xmljs.node
 → /Users/myusername/Documents/Personal/MyApp/compiled/6.5.0/darwin/x64/xmljs.node

有人可以帮我让 libxml-xsd 在我的应用程序配置下运行吗?这和 webpack 有关系吗?如果是这样 - 我如何更改其配置以引入适当的文件?

【问题讨论】:

【参考方案1】:

您可以使用来自GitHub issue 的代码解决bindings.js 的问题。

网络包

module.exports = 
  plugins: [
    new webpack.IgnorePlugin(/^mock-firmata$/),
    new webpack.ContextReplacementPlugin(/bindings$/, /^$/)
  ],
  externals: ["bindings"]

问题是libxml-xsd 包是用于服务器端的。我想你会在这个之后遇到另一个问题。它有像 require('fs'); 这样的依赖项,不能在前端使用。

【讨论】:

但是我可以使用 ipc 来制作后端句柄,你认为对吗? @RenegadeAndy 我不知道 IPC,但你绝对可以在 node.js 上使用这个库并发送 http 请求。 那么我应该在我的项目中添加哪个文件? @RenegadeAndy 我的回答中的代码应该添加到 webpack 配置中。【参考方案2】:

我认为您需要在包含本机模块的文件夹上使用 asar-unpackasar-unpack-dir 参数。

看起来它只需要一个字符串,有关 glob 示例,请参阅asar-unpack-dir 的 API 文档。 (一个字符串限制似乎是 asar 模块限制,如果您认为它应该作为数组工作,您需要在他们的存储库中提交问题。)

【讨论】:

以上是关于在电子应用程序中使用 libxml-xsd 失败并出现绑定错误的主要内容,如果未能解决你的问题,请参考以下文章

电子构建失败并出现 ERR_ELECTRON_BUILDER_CANNOT_EXECUTE

用于失败测试用例的 J Meter 电子邮件配置

提交数据库php后如何处理电子邮件发送失败

批量电子邮件失败,出现 421 4.7.0 稍后再试

如果在批处理过程中 nmake 失败,则返回错误和电子邮件

发送电子邮件传输 Mandrill 失败