-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fs and appPath looking in electron distribution in node_modules #374
Comments
@jefffriesen I think you are looking for import path from 'path'
import { remote } from 'electron'
const irisPath = path.join(remote.app.getPath('appData'), '/yourappsname/iris.json') |
@vinnymac I'm not sure this is right, at least for my use case. But I may be thinking about this wrong. I'm trying to bundle and distribute large data files with my app. This code: import path from 'path';
import { remote } from 'electron';
const irisPath = path.join(remote.app.getPath('appData'), '/electron-react-boilerplate/iris.json');
console.log('irisPath: ', irisPath);
const irisData = JSON.parse(fs.readFileSync(irisPath, 'utf8'));
console.log('irisData: ', irisData); produces irisPath: /Users/jeffers/Library/Application Support/electron-react-boilerplate/iris.json
fs.js:640Uncaught Error: ENOENT: no such file or directory, open '/Users/jeffers/Library/Application Support/electron-react-boilerplate/iris.json' Which is consistent with their documentation. Using What's strange is this worked properly on a previous version of this boilerplate. I'm guessing it was a change the Electron 1.0+ API, but I don't know how to fix it. |
@jefffriesen it works for me in my projects with the latest version of electron. I noticed that above you wrote the It looks like you are using OS X, so you can just open finder and type SHIFT+CMD+G and then type the path to your project, such as I think that the reason you got the error you received is because it uses the This might also only occur when you run package.js. As I believe that may be creating the directory. Interestingly it seems that the folder doesn't exist in the development environment. I guess you need to check if the folder exists and create it yourself in that location during development. Kind of odd that you would need to do that, but it would certainly fix the issue, and any other issues that would arise from the folder not existing. |
@vinnymac et al, I'm confused though - why would there be any data in I'm developing the app storing data in I ran an experiment: // ran in both old app and new app (unpackaged in dev environment):
console.log("remote.app.getAppPath(): ", remote.app.getAppPath())
console.log("remote.app.getPath('appData'): ", remote.app.getPath('appData'))
// latest electron-react-boilerplate
remote.app.getAppPath(): /Users/jeffers/git/forks/electron-react-boilerplate/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar
remote.app.getPath('appData'): /Users/jeffers/Library/Application Support
// older app based on electron-react-boilerplate
remote.app.getAppPath(): /Users/jeffers/git/jefffriesen/oldAppName // works correctly
remote.app.getPath('appData'): /Users/jeffers/Library/Application Support I have 3 apps based off an older version of this repo and all use In the master branch of Electron, the docs state that
(https://github.com/electron/electron/blob/master/docs/api/app.md#appgetapppath) I searched the release notes of Electron for Thanks for helping me track this down. |
@jefffriesen You bring up a good point about asking why it is created. I haven't figured out when or why. However, I did some testing of my own to see if I could provide you more information. I noticed that during development I do not see any folders added to the When in development I believe electron prebuilt creates a tiny electron app in If you package an app with the latest version do you see the folders created? As an aside, you can probably solve this a different way. I think you are just trying to get access to some json data in electron, you shouldn't even need the path. Using webpack you should be able to just simply require the json file in your directory and then when it builds main or your bundle it will include the json as a module in the final bundle. Something like import iris from './data/iris.json' Unless I misunderstood what it is you were trying to do. If you need to keep writing new json for some reason, then you will probably need a place to store that data, and then we will be back to discussing paths. If all you need is the data, then requiring it should work. |
@vinnymac You're right - a folder is created in the Unfortunately, Now that I know Electron builds a set of files on startup in Looking back on my first post on this thread, it still seems relevant:
So I'm still stuck but I also think other people are going to run up against this. This seems like it's due to a configuration change that's happened in the app in the last 6 months but I don't know where to test from here. |
@jefffriesen If you are failing to include the json in webpack I would add the json to the noParse list. It will tell webpack that it is a large dependency and to leave it untouched. Hopefully that will help you see better results with larger files. This way you can include it in the bundle. If for some reason that fails you, you can try requiring the json. According to what I can find online, including this, node.js appears to be capable of requiring Getting the right path shouldn't be that bad, using relative require paths should make it easier. If you know the relative paths between the file you are requiring it from and the I agree that something changed, and is funky. It would be awesome to figure out what and why, but I am still trying to think of other ways of solving your problem without figuring out what changed. If you'd like me to try to test anything for you let me know, I'd be willing to post my results as well. |
@vinnymac great idea to tell webpack not to include the json. That would likely allow me to at least load the files. It's not as nice as I still want to get the path stuff figured out for me and all of us using this repo. I was looking at other electron boilerplate repos to see how they configured the packaging. Here's one but nothing stands out as the solution: https://github.com/SimulatedGREG/electron-vue/blob/master/template/config.js. Maybe it does to you |
@jefffriesen the first thing that pops out to me is the arguments in ignore. The docs on electron-package state that those files won't be included in the app bundle. Perhaps you could temporarily ignore nothing, and see how that changes things. Just comment out these lines. If this works, I'd imagine it is as simple as placing your files in a path that is just not ignored. In any case, attempting to see if any changes have been made around the |
@vinnymac your lead on what's included in I really want to solve this Data, as before, is stored in
I tested different start configurations:
Ok, so that works. The difference between "start": "cross-env NODE_ENV=production electron ./",
"start-hot": "cross-env HOT=1 NODE_ENV=development electron -r babel-register -r babel-polyfill ./main.development", Thinking it had something to do with NODE_ENV=development, I tried
That leaves the culprit as one or both of these:
mainWindow.loadURL(`file://${__dirname}/app/app.html`); But that doesn't seem like it would do it. Maybe it's what's not being set in main.development.js but is in main.js? Or maybe it's a babel issue, but that seems less likely. Open to suggestions |
Hrmm, well if When I am near my computer I'll try to do what you did above and post my findings, and see if I get anywhere with it.
EDIT@jefffriesen I added a couple of additional logs such as Results from a fresh install of Results from a fresh install of your |
Quick followup:
Branch with notes and minimal code are here: https://github.com/jefffriesen/electron-react-boilerplate/compare/master...jefffriesen:fs-test2?diff=unified&name=fs-test2 |
When looking at other helpful projects such as If you look at the docs for electron-builder you can see the options that they allow. I think that when building electron for production you need to tell it you are including this json as an extra file. If you don't want to require it via webpack and carry it along inside the I think electron is expected to be packaged with the necessary files, but you aren't passing these necessary files in when starting electron. For starters, move List all files under the root of the archive: const fs = require('fs')
fs.readdirSync('/path/to/example.asar') EDITAfter running this on the appPath In a production packaged build this is what I see from listing You can also use In production, these are the paths that were logged. I also added You would think including a json file would be as simple as including an image file or any other dependency of the project. For some reason I am convinced their is an easier way than all of this though. When looking at other apps I have built before using the same setup as this project, the final mac |
@vinnymac great research and info. I'm learning a lot. First off, I tested I did move data/iris.json out into the main application folder and adjusted the path for I doubled checked to see if the build step was writing to any files inside The problem I think has to be in the step going from @jhen0409 @chentsulin @krzkaczor First of all thanks for doing so much for this repo. I'm stuck on a problem using You guys worked on this file: https://github.com/chentsulin/electron-react-boilerplate/blame/master/webpack.config.electron.js#L12 which creates Here is a simple setup on a clean, recent branch of this repo: const irisPath = `${appPath}/app/data/iris.json`;
const irisData = JSON.parse(fs.readFileSync(irisPath, 'utf8'));
console.log('appPath: ', appPath);
Do you all have any idea why Here is a compare of my fork and master: master...jefffriesen:fs-test2 |
@jefffriesen I decided to investigate this issue from another point of view. This time I took the repo we had that was working, quick-start, and then I took your branch fs-test2. I made your branch more and more like quick-start until it worked as you intend it to. I believe I have discovered the cause. First I replaced the This also did not fix the issue. Finally, I created a It worked. So apparently this is because electron needs to see your package.json for the name of the app. I tried other file names and paths such as Trying this helped me understand it.
The result should be the same path as before, but this time with /example appended. One more reason to use the two separate package.json configuration that I use in some of my other projects, kind of a shame that it can't be done with one. Relevant Issues electron/electron#3204 |
@vinnymac Great detective work. This is the money quote from that stack overflow link (which is what you're saying too):
Doesn't that sound like a poor design choice by Electron? I know lots of people are working on Electron and using it in huge apps, so maybe there is a really good reason for it. It doesn't smell right and it's really subtle. So what's the next step for this? Is there a PR that would be appropriate here? Or will this be made irrelevant by this PR: #310 |
@jefffriesen I am not sure what the next step here is. Obviously const remote = require('electron').remote;
const appPath = process.env.NODE_ENV === 'production' ? remote.app.getAppPath() : __dirname This way development mode falls back to being the root directory where the I'd love to see someone come up with a better solution, as I am not a big fan of this one. |
I've been thinking along the lines of the NODE_ENV conditional as well. You're right though it's a little hacky. It's fine for me personally for now since I know the issues. I hope other people don't get stuck on this. It sounds like electron-builder is a better packager anyway and that PR seems fairly close. Should we keep this ticket open while we wait for that? |
Was this issue resolved? |
No. We currently have a hacky workaround: const remote = require('electron').remote;
const appPath = process.env.NODE_ENV === 'production' ? remote.app.getAppPath() : __dirname But something in main.development.js is breaking where |
What happens when you |
These don't work:
This works:
You can see the tests I ran higher in the comments: #374 (comment) I think the key finding from @vinnymac is that when you point Electron at a script directly, it ignores You can see that with these scripts: "start-hot": "cross-env HOT=1 NODE_ENV=development electron -r babel-register -r babel-polyfill ./main.development",
"start": "cross-env NODE_ENV=production electron ./", You can see his comment here: #374 (comment) |
I wonder if it makes sense to use 2 package.json files like @vinnymac suggested. Here is an electron boilerplate repo that does it:
But for us it would also solve our |
|
Looking forward to it
|
@jefffriesen does this issue persist in the latest release? |
I'll look into it. Thanks |
@amilajack Unfortunately it's still not fixed even with the dual package.json setup. I've merged upstream and reinstalled all node modules detailed here (#400)
It's still looking in the You can see the diff compared to master here: jefffriesen/electron-react-boilerplate@master...jefffriesen:fs-test2 Packaging the app (on Mac) now gives a new error
|
@jefffriesen can you point to the source of this project? |
Here is the diff of the branch vs master on my fork of the repo: jefffriesen/electron-react-boilerplate@master...jefffriesen:fs-test2 |
@jefffriesen was this resolved? |
I'm not in front of my computer for a couple days but I'll check when I get back. Thanks.
… On Dec 9, 2016, at 5:10 AM, Amila Welihinda ***@***.***> wrote:
@jefffriesen was this resolved?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@amilajack this isn't fixed yet. I'm kind of surprised no one else is running into problems using Here is a simple example of it breaking, based off the latest master: It's basically this: const remote = require('electron').remote;
const appPath = remote.app.getAppPath();
const irisPath = `${appPath}/data/iris.json`;
const irisData = JSON.parse(fs.readFileSync(irisPath, 'utf8'));
console.log('irisData: ', irisData); The error I get with
I don't think it should be looking in the
|
@jefffriesen if I were you I would open an issue in the electron or electron-builder repositories. This issue seems to be specific to starting electron from a specific path where a package.json is, and is not particular to electron-react-boilerplate. I would link back to this discussion for education on the problem. |
How about |
@JackieLs Running the following before we use Produces the following for me Production will throw an error when we use Same reason why the fix we came up with above works, because it checks whether or not you are using production or development first. I think this issue needs to be brought up with |
I believe this issue needs to be revisited as there is really a 3rd "state" that has not been addressed. Specifically, I am having the exact problem listed above and have used the logic of "production" and "development" for file access. Here is my example:
This allows me to call either So there really are 3 states: In its current form, you have to sacrifice one of the production states unless I've missed something. |
@mtompkins do you have a reproduction? The above tests were run with |
@vinnymac I'll strip up an example as soon as I can. Thanks for the reply. |
Ok - so after referencing much of the above and a bit more research... Path looks like: Following down the references to
this causes the directory to be copied including preserving the path in to the That can be checked by a This then allowed me to use references that were consistent across dev, prod, and package. HTH someone else |
I'm unable to load a file using
appPath
because it's looking inside the Electron.app inside of node_modules:electron-react-boilerplate/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar
Here is an example: jefffriesen@961fd0e
I've compared this with the vanilla
electron-quick-start
app and it doesn't have this behavior:jefffriesen/electron-quick-start@8cf2650
Is there an alternative to
appPath
that I should be using, or is possibly a configuration problem in this repo?Thanks
The text was updated successfully, but these errors were encountered: