Reputation: 335
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?
Upvotes: 1
Views: 2960
Reputation: 2073
I followed the advice here and got it to work.
Basically, replacing all occurrences of __dirname
with process.cwd()
got it to work.
NOTE: A simple find-and-replace will not work. You also need to remove any extra relative paths like "../../" from all occurrences. So
path.join(__dirname, '../../', 'uploads')
will becomepath.join(process.cwd(), 'uploads')
Conclusively,
.env
load just fine if placed next to the executable.Upvotes: 1
Reputation: 335
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
Upvotes: 3