Hi!
In my project I'm reading an external file (a private rsa key with extension .key) with this code:
fs.readFileSync('myfolder/my_private_key.key');
I'm able to build the project with pkg 4.3.4, but when I run the final binary file created by pkg I get this error:
"Error: ENOENT: no such file or directory, open 'myfolder/my_private_key.key'",
" at Object.fs.openSync (fs.js:646:18)",
" at Object.fs.openSync (pkg/prelude/bootstrap.js:483:32)",
" at Object.fs.readFileSync (fs.js:551:33)",
" at Object.fs.readFileSync (pkg/prelude/bootstrap.js:679:36)",
(...)
If I copy this file next to the binary created by pkg, I can read it, otherwise no. So I suppose that It's not included in the final binary file created by pkg.
How can I fix this?
fixed using
const certPath = path.join(__dirname, '../myfolder/my_private_key.key');
const cert = fs.readFileSync(certPath);
and in my package.json I added:
"pkg": {
"assets": [
"public/**/*",
"*.crt",
"*.key"
],
"targets": [
"node8"
]
},
@Ks89 how about using fs.writeFile I am getting an error
I never tried, sorry
Well, I was able to fix it by reading the docs.
I added a few lines to the source code.
// Set the path of the project folder base on whether it is run with nodejs or as an executable
let project_folder;
if(process.pkg){
// It is run as an executable
project_folder = path.dirname(process.execPath)
}else{
// It is run with nodejs
project_folder = __dirname
}
Then I change the call to fs.writeFile to
fs.writeFile(path.join(project_folder, 'assets', 'my_image.png'))
This issue comes up on Google when searching for node pkg error reading from file. I get this error message when using rcedit with the generated Windows executable to set the executable icon. Prior to patching the executable's resources to set the icon, it works just fine as generated by PKG. After applying the icon resource, this error happens. There are some other links about this error online, but specifically related to PKG, this issue ranks the highest so I figured this information should go here. Also, it is kind of deceiving, but the icon is applied just fine to the binary, it's just that the rest of the binary is wrecked.
FWIW I have not found a better way to set the Windows executable icon.
Most helpful comment
fixed using
and in my package.json I added: