My app uses fs.appendFileSync() and fs.writeFileSync() to write to log files and save json data respectively, but both fail with similar errors below:
// This is my console.log() output
Thu Feb 22 2018 20:56:12 GMT-0600 (CST) [ERROR]: {"errno":-2,"code":"ENOENT","path":"/snapshot/citysensor/log/citysensor.log","pkg":true}
// stdout error
(node:98708) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: File or directory '/**/citysensor/log/citysensor.log' was not included into executable at compilation stage. Please recompile adding it as asset or script.
(node:98708) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
If I "include into executable at compilation stage" (I DO NOT want to do this in prod), then I just get another error:
(node:99009) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Cannot write to packaged file
(node:99009) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
My app also generates save data into a json file, yet that fails as well with pkg... what's going on? These files have to be generated on the fly, so they never exist initially. I researched https://github.com/zeit/pkg/issues/286, but even with process.execPath, while it "works", the resulting path is not what I want because I can't seem to modify the dir path.
UPDATE
For the log file, using fs.appendFileSync(), I simply tossed away my path.join() (https://github.com/zeit/pkg#detecting-assets-in-source-code) and tried saving the log file in the same directory as the executable (i.e. fs.appendFileSync('citysensor.log')). This works, but is not ideal. I would like these log and json files to be written to a specified path, not simply put in same directory as node app.
For example, these do not work:
const rootDir = path.join(__dirname, '..')
// or const rootDir = path.join(process.execPath, '..')
fs.writeFileSync('${rootDir}/ap.json')
I also wonder if this has anything to do with my webpack config, since pkg compiles the resulting webpack output of server.js:
module.exports = {
target: 'node',
node: {
fs: 'empty',
__dirname: false, // required for linux
},
...
}
Does anyone have any update on this ?
I am running into this issue and seems crucial for packaging my app.
Can refer to @381 for igorklopov's comment.
A little late but
path.resolve(process.cwd(), /# etc. #/)
seems to work for me (using both node file.js and using the exe's generated from pkg)
Most helpful comment
A little late but
seems to work for me (using both
node file.jsand using the exe's generated from pkg)