Electron file path in package issue - node.js

I wonder how to get the correct path to file what should be loaded by fs.readFile:
File to load is in the app's root directory.
Starting the app in cli with npm start
var data = fs.readFileSync('settings.json');
works
After packing and starting electron.exe it will lead to
'Error: ENOENT: no such file or directory'
I can fix this for the packaged app with loading by
var data = fs.readFileSync(path.join(process.resourcesPath, 'app', 'settings.json'));
but then the file isn't found runing the app by npm start.
What is the correct way to determine the path to a file in any environement?

Related

How to resolve path to a file within node_modules

Problem loading a file using relative path when my node app is initialised using a another node app
I have created an npm which relies on a file stored relative to project root. something like this
index.js
- res
- config.json
Now I read the config.json using following code
const pathToConfig = path.resolve(__dirname, '../res/config.json')
This works great locally.
But in my prod setup this app is initialised by another node app.
And __dirname resolves to root of that app so all my logic to find config.json get messed up.
Is there any way I can read the file without worrying about how node app was initialised?
Have you tried the command process.cwd()? It is almost the same as __dirname but does differ slightly.

How to path to an image file in an electron app?

I am creating an electron app and packaging for distribution with electron-builder for windows and Mac. This app creates a folder and some pdfs inside varying based on user input. The pdfs also use an image, which as a node app I was keeping in the root folder of the app.
I managed to write to the desktop using an absolute path.
if (!fs.existsSync(`/Users/${user}/Desktop/2019 Certificates`)){
fs.mkdirSync(`/Users/${user}/Desktop/2019 Certificates`);
}
but when I use this relative path
stampandseal.png
I get the following error:
I expect it to find the png relative to the js file, however I get the following error:
fs.js:121 Uncaught Error: ENOENT: no such file or directory, open 'stampandseal.png'
If I understand your issue correctly, you are trying to copy an image from within the app bundle to the user's desktop. Use __dirname to reference the directory your code is executing in, and then build the path off of that.
The code below is used by my main.js file, which is in the directory containing my app directory. I use upath to build the path and jetpack instead of fs for copying
var fromPath = upath.join(__dirname, "app", "assets", "image.png");
jetpack.copy(fromPath, toPath, { overwrite: true });

no such file or directory, scandir '/sql/' issue var sql = new SQLBuilder('SQLServer'); on creating the object of json-sql-builder2 module

I'm getting this below error on deploying my aws lambda package on node 8.10.0 but it works fine in my local windows.
let modules = tools.walk(path.join(__dirname, `../sql/`));
on logging the _dirname it was showing '/' even though its is 'var/task'
lambda uploaded package has src
src/index.js
sql
sql/somemodules
this folders and files inside the uploaded package.
FYI: lambda package has bundled using webpack.
2019-05-09T04:23:22.996Z 6010d98c-2788-4c96-b972-49361876948c Error: ENOENT: no such file or directory, scandir '/sql/'
at Object.fs.readdirSync (fs.js:904:18)
at Object.walk (/var/task/src/index.js:144899:16)
at SQLBuilder._loadModules (/var/task/src/index.js:144709:23)
at new SQLBuilder (/var/task/src/index.js:144298:8)
at handler (/var/task/src/index.js:76189:15)

pug.compileFile('./views/transactions/html.pug'); Heroku Node Express. Error: ENOENT: no such file or directory, open

Error: ENOENT: no such file or directory, open './views/transactions/html.pug'
Running my application locally I don't have a problem, once I push to Heroku I get the above error. Locally I am using nodemon to serve my express server. I'm using pug to compile some html to send emails. This is the function that is causing the problem.
pug.compileFile('./views/transactions/html.pug')
Using node Express, with React, serving the static/build files from the same server.
My suspicion is that I need to serve the 'views' directory as when I am running locally my nodemon will be doing this, however, I 'm not sure how to do this.
This is a view of my server.js file.
I have a solution to the error message. I got this from this post about node/heroku filepaths.
The issue was that locally my file path was pointing to the correct file but the online/heroku server needed a different filepath. I have included a picture of the file structure for what it's worth.
function createEmailFilePath(filename) {
const newpath = process.env.NODE_ENV === 'production' ?
path.resolve('emails', 'transactions', filename) :
path.resolve('..', 'emails', 'transactions', filename);
return newpath;
}

Iin meteor deploy server Error: ENOENT, no such file or directory

in my meteor application I am using node fs to write the file, but when I deployed my application at meteor.com server then I am getting error in meteor logs.
WARNING Error: ENOENT, no such file or directory 'testaudio/server/uploads/6_40_16_340.pcm'
at Object.fs.openSync (fs.js:432:18)
fs = Meteor.npmRequire('node-fs'); in my server and i mentioned the version in packags.json
I am writting file inside /server/uploads/file_name in my application.
I dont know why I am unable to write the file at server. event I tried with full path by using
fullPath = meteor_bootstrap.serverDir that gives path like :-
/appDir/.meteor/local/build/programs/server

Resources