简体   繁体   English

在使用“pkg”编译器将整个服务器捆绑到可执行文件后,如何要求 json 数据到 Node.js?

[英]How to require json data to Node.js after bundling the whole server into an executable with the "pkg" compiler?

In normal node.js environment which I run by "npm start", I can always just require/import JSON and the data inside the file will be received.在我通过“npm start”运行的普通 node.js 环境中,我总是可以只需要/导入 JSON 并且将接收文件中的数据。

For an example, if I have "./Assets/port.json" and it includes {"port": 3000} When I import it and access the data I will get port 3000, and if the data in the port.json file is changed to port:3001, I will then get port 3001 when I import it.例如,如果我有 "./Assets/port.json" 并且它包含{"port": 3000}当我导入它并访问数据时,我将获得端口 3000,如果 port.json 文件中的数据改成port:3001,我导入的时候会得到3001端口。 Working fine.工作正常。

But once I package the file using "pkg server.js" and I get my executable, it seems the json data is written to the executable binaries and when I then change the data in the json file to something else, I still get the same data I had in the json file during the compilation of the node.js code into an executable.但是一旦我使用“pkg server.js” package 文件并且我得到了我的可执行文件,似乎 json 数据被写入可执行二进制文件,然后当我更改 Z466DEEC76ECDF5FCA6D38571F6324 文件中的数据时,我仍然得到相同的文件在将 node.js 代码编译为可执行文件期间,我在 json 文件中拥有的数据。

How can I prevent the json data from being compiled into the binaries and make the json data be imported once my node.js code is compiled into an executable?一旦我的 node.js 代码被编译成可执行文件,如何防止 json 数据被编译成二进制文件并导入 json 数据? It's really important for me as I am creating a clientside application which will run a server on their LAN, so I must be able to have dynamic ipv4/port imports from the json file possible.这对我来说非常重要,因为我正在创建一个将在他们的 LAN 上运行服务器的客户端应用程序,所以我必须能够从 json 文件中导入动态 ipv4/端口。

From the pkg doc :从 pkg文档

On the other hand, in order to access real file system at run time (pick up a user's external javascript plugin, json configuration or even get a list of user's directory) you should take process.cwd() or path.dirname(process.execPath).另一方面,为了在运行时访问真实的文件系统(选择用户的外部 javascript 插件,json 配置,甚至获取用户目录列表),您应该使用 process.cwd() 或 path.dirname(process.执行路径)。

Code such as:代码如:

require("./Assets/port.json")

will automatically be replaced with the loading of a bundled asset in the pkg as that's what pkg does.将自动替换为 pkg 中捆绑资产的加载,这就是 pkg 所做的。 It scours your source code for the loading of things using certain rules.它使用某些规则搜索您的源代码以加载事物。 If, however, you load it with something like this:但是,如果您使用以下内容加载它:

const config = require(path.join(path.dirname(process.execPath), "port.json"));
console.log(config.port);

And make sure that the file port.json is located in the same directory as your packaged executable after your app is installed somewhere, then pkg will leave this code along and allow you dynamically load the config file from the same directory as the executable is located.并确保文件port.json在您的应用程序安装到某处后与您打包的可执行文件位于同一目录中,然后 pkg 将保留此代码并允许您从与可执行文件所在的同一目录动态加载配置文件.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM