如何从nodejs项目中生成.exe



我正试图从我的nodejs项目中生成.exe。我使用的是pkg命令,但当我在cmd中键入pkg package.json时,我会收到以下消息

> pkg@5.8.0
> Warning Babel parse has failed: This experimental syntax requires enabling the parser plugin: "importAssertions". (1:39)
> Warning Cannot include directory %1 into executable.
The directory must be distributed with executable as %2.
%1: node_modulespuppeteer.local-chromium
%2: path-to-executable/puppeteer
> Warning Cannot include directory %1 into executable.
The directory must be distributed with executable as %2.
%1: node_modulespuppeteer.local-chromium
%2: path-to-executable/puppeteer
> Warning Failed to make bytecode node10-x64 for file`

我正在导入带有以下内容的JSON文件:

import config from "../../config.json" assert {type: "json"}

这是我的package.json:package.json

我导入config.json的方式不对吗?我的应用程序打包方式不对吗?我做.exe的方法不对吗?

好吧,我会给出一个答案,这样我就可以编辑它,并且比评论更彻底,尽管我不能确定这是完整的解决方案,但这肯定是其中的一部分。

您需要安装importAssertions Babel插件。

您可以通过在cmd行中运行以下行来做到这一点:

npm install --save-dev @babel/plugin-syntax-import-assertions

此外,请注意,语法import config from "../../config.json" assert {type: "json"}是实验性的,还不是实际标准的一部分。这就是为什么你需要安装插件才能使用它。我建议你简单地将config.json修改为config.js,并制作js文件:

module.exports = {
//whatever json was originally in the config file
}

这意味着你将不再需要实验语法,除非配置文件是一个直接的json文件,否则会更容易。这也意味着,如果/当实验语法发生轻微变化时,您不必修复问题。

最新更新