Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Error with external files read with fs.readFileSync are not included in pkg bundle #521

Closed
Ks89 opened this issue Sep 7, 2018 · 6 comments

Comments

@Ks89
Copy link

Ks89 commented Sep 7, 2018

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?

@Ks89
Copy link
Author

Ks89 commented Sep 10, 2018

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 Ks89 closed this as completed Sep 10, 2018
@Jucesr
Copy link

Jucesr commented Sep 11, 2018

@Ks89 how about using fs.writeFile I am getting an error

@Ks89
Copy link
Author

Ks89 commented Sep 12, 2018

I never tried, sorry

@Jucesr
Copy link

Jucesr commented Sep 12, 2018

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

@TomasHubelbauer
Copy link

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.

@jeroen-van-dijk
Copy link

I got it fixed my listing the exact path in the package.json.

"pkg": {
        "scripts": "index.js",
        "assets": [
            "key_folder/server.key",
            "key_folder/server.cert"
        ],
        "outputPath": "dist"
    },

see #893 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants