如何从Node.js项目生成.exe文件

3

我正在尝试从我的Node.js项目创建.exe文件。我使用pkg命令,但当我在命令提示符中输入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_modules\puppeteer\.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_modules\puppeteer\.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文件的方式是否有误?

所以...为什么不按照警告的指示启用解析器插件import assertions呢?https://www.npmjs.com/package/@babel/plugin-syntax-import-assertions - Sam Spade
这就是我想做的,但我找不到如何实现。 - Timen
你是否使用npm安装了该模块并将其添加为开发依赖项? - Sam Spade
1个回答

0

好的,我会写一个答案,这样我可以编辑并进行更详细的解释,虽然我不能确定这是完整的解决方案,但这肯定是其中一部分。

您需要安装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 文件真正重要,否则会更容易。这也意味着如果/当实验性语法略有变化,您将不必修复任何东西。


我已经将json作为module.export导出。现在消息已经改变:> 警告Babel解析失败:此实验性语法需要启用解析器插件:"importAssertions"。(1:39)已被删除,但其余部分仍然存在。 - Timen
@Timen,它是否生成了.exe文件?因为另外两个警告只是说你需要包括某些文件夹,这不应该是一个问题。 - Sam Spade
@TImen 试一下这个线程,他们似乎也遇到了同样的问题。https://github.com/vercel/pkg/issues/721 - Sam Spade
好的,我找到了问题所在,他想要在可执行文件的相同路径下拥有“puppeteer”文件夹。因此,我将puppeteer放在了“node_modules”之外,但现在我不明白应该如何导入puppeteer。我得到了这个错误Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import 'my_path\codeSource\puppeteer' is not supported resolving ES modules imported from my_path\codeSource\src\service\launchPup.js - Timen
@Timen将其放在外部和内部都可以。在构建文件夹中放入node_modules文件夹的副本,但是不要从node_modules中删除它。 - Sam Spade
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接