Search code examples
node.jsexeyarnpkg

Node JS pkg executable not able to read files from outside?


I have a JS script which I package into an executable using pkg --public filename.js

I want to read data from a file like this:

let filename = __dirname + "/data/streets_profiles.csv" 
fs.createReadStream(filename)
.pipe(parse({ delimiter: ',' }))
.on('data', (r) => {
    //console.log("r: ", r);
    data.push(r);        
})
.on('end', async () => {
 //... do something

While this works well when I just run the script with node filename.js in the VS code powershell, when I package the script into a executable I get this the error from executable

I found a solution, which would be to add the csv file path I want to read into package.json assets, but I would like to be able to change the data in this csv file without creating another executable, and for it to not be locked in the executable as a asset but rather as a standalone editable file which can be read by the exe, is there any way i could achieve this or any better way to create a executable without these limitations?


Solution

  • So after a long research I found out that the packaged executable is located in C:/snapshots for me so getting __dirname was useless, but path.dirname(process.execPath) solved my issue as it can get the actual location of the file and therefore actions with files in the same directory are enabled