Electron-packager: Unable to load module that isn't specified in Electron app's package.json

Created on 28 Feb 2018  路  12Comments  路  Source: electron/electron-packager

I'm trying to package an electron app which resides in electron-app/ folder in my project folder. The modules are installed in ../node_modules folder.

I have a module that is required in the preload script(electron-app/preload.js) and attached to the window object. After packaging this app, electron throws the said error with module not found, but if I install the module in the electron-app folder, everything works fine.

I've tried to push the ../node_modules to module.paths but to no avail.

question

All 12 comments

馃憢 Thanks for opening your first issue here! If you have a question about using Electron Packager, read the support docs. If you're reporting a 馃悶 bug, please make sure you include steps to reproduce it. Development and issue triage is community-driven, so please be patient and we will get back to you as soon as we can.

To help make it easier for us to investigate your issue, please follow the contributing guidelines.

In order to debug your problem further, we need a minimal testcase to reproduce your problem. Using the electron-quick-start repository as a base, could you please create a minimal Electron app that illustrates the issue you described, and post a link to it here?

@malept https://github.com/navdeepio/electron-quick-start

Try packaging the app in electron-app folder and running it

I have some additional questions:

  • What command line arguments are you passing?
  • What directory are you running electron-packager from?
  • Why are there two package.json files?

electron-packager electron-app/ from electron-quick-start/ folder

  • Two package.json files are there for testing, but feel free to remove the one inside electron-app/

The problem is that Electron Packager reads the package.json file inside of electron-app, so it prunes away any module that's not specified in electron-app/package.json. If you need to keep instascan, you need to add it to that package.json.

@malept tried that too, still not working. Please open it after verifying

I ran (cd electron-app && npm install --save instascan) and it did this:

diff --git a/electron-app/package.json b/electron-app/package.json
index 8392c12..ccffd28 100644
--- a/electron-app/package.json
+++ b/electron-app/package.json
@@ -15,5 +15,8 @@
     "demo"
   ],
   "author": "GitHub",
-  "license": "CC0-1.0"
+  "license": "CC0-1.0",
+  "dependencies": {
+    "instascan": "^1.0.0"
+  }
 }

Then I ran electron-packager electron-app, and then electron-quick-start-linux-x64/electron-quick-start. I didn't get an error about instascan or mykey.

In fact, I verified in the open devtools console that window.mykey contained Instascan.

ok, that's the point - instascan is installed and available in the parent directory(electron-quick-start). So to not have two yarn.lock files(one in parent dir and one in electron-app/) I've removed electron-app/node_modules, which I request you to remove yourself

At this point, I think you should make your webapp into a (private) module and add that into dependencies in electron-app/package.json. I suggest looking at yarn workspaces to make sure that there's only one yarn.lock, etc. (Although there is currently a bug with workspaces and pruning at the moment.)

@malept I agree. But out of curiosity, shouldn't electron-packager be able to resolve the module from node_modules that's one dir up? Especially since module.path contains electron-quick-start/node_modules or ../node_modules?

How would it know about those folders? You've specified that the app resides in electron-app, not its parent directory. Electron Packager doesn't copy anything in the app's parent directory.

Was this page helpful?
0 / 5 - 0 ratings