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?
因此,经过长时间的研究,我发现打包的可执行文件对我来说位于 C:/snapshots 中,所以获取__dirname
是没用的,但是path.dirname(process.execPath)
解决了我的问题,因为它可以获得文件的实际位置和因此启用了对同一目录中的文件的操作
I followed the advicehere 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.