0

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.

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. 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.

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? 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.

2
  • How are you loading the JSON file? Can you show that code? Depending upon how you're doing it, there are ways to bypass pkg so you can load the file dynamically rather than have it come from within the package.
    – jfriend00
    Aug 19, 2022 at 17:05
  • @jfriend00 I load it with the standar require("") function. E.G. const port = require("./Assets/port.json").port; Aug 19, 2022 at 17:10

1 Answer 1

0

From the pkg doc:

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).

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. 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.

1
  • I want to thank you for this answer so much. Today I somehow missed details in the documentation of the PKG Compiler. It states that any "require" call with relative path & string literal imports will include the called file within the compiled binary. But if you use process.execPath or process.cwd() it calls those functions at runtime as well which lets you import dynamic data. I ended up using equire(path.join(process.cwd(), "./Assets/port.json")); and just had to change all my require paths relative to the root directory due to process.cwd() call. Now my app is working perfectly! Thanks!! Aug 19, 2022 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.