Skip to content
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

"Uncaught Error: Cannot find module" when using asar with native modules #217

Closed
bitwombat opened this issue Dec 13, 2015 · 56 comments
Closed
Labels
question ❓ Question about using Electron Packager. Not supported in this issue tracker.

Comments

@bitwombat
Copy link

I have a require('my-module') in a renderer js file. It lives in my-app/node_modules (though I've tried putting it in my-app/.)

It works fine when I run electron . from my-app.

However, once packaged for win32 (created on Ubuntu 14.04), on launch of the .exe I see "Uncaught Error: Cannot find module my-module" in the browser/devtools console.

I've tried require('./my-module')

Am I doing something wrong?

This is with electron 0.36.0, npm-packager 5.1.1

@max-mapper
Copy link
Contributor

It's most likely that your module is not getting found by node/npm. I have some docs on how this works here: https://github.com/maxogden/art-of-node#how-require-works. Keep trying until it works, I don't think anyone has reported this particular issue before so it's likely something with your setup.

@bitwombat
Copy link
Author

Thanks @maxogden. I'm going to drum up a simple testcase. I discovered module.paths.push, but it was crunch time and my approach was a lot of spray n' pray. Back to you soon.

@malept malept added the question ❓ Question about using Electron Packager. Not supported in this issue tracker. label Dec 20, 2015
@nmalcolm
Copy link

I'm facing the same issue. I have an Electron app which loads a local Express server. The page loads a single Javascript file.

var url = require('url');
var querystring = require('querystring');
var process = require('process');
var path = require('path');

module.paths.push(path.resolve('node_modules'));
module.paths.push(path.resolve('../node_modules'));

var request = require('request');
// ...

On Ubuntu this works fine with electron . and by running the executable. On Windows running the executable fails with "Uncaught Error: Cannot find module 'request'". If I remove the module.paths.push() lines it fails on Ubuntu too.

Other than the Express server everything is pretty standard. I'm only trying to load npm installed modules from the default node_modules directory.

I should note it fails with and without asar. I'm not sure if this is a bug with Electron, Electron Packager, or I'm just doing something wrong. I'd like to think node_modules would be loaded without having to mess around with module.paths.

Additional info:

Electron version: 0.36.2 (electron-prebuilt)
Electron Packager version: 5.2.0
Node version: 0.10.41
NPM version: 2.14.14
Build command: electron-packager . app --platform=win32 --arch=x64 --version=0.36.2 --overwrite --asar=true

@nmalcolm
Copy link

Ok, so I've done a little experimenting and this is what I've come up with:

module.paths.push(path.resolve('node_modules'));
module.paths.push(path.resolve('../node_modules'));
module.paths.push(path.resolve(__dirname, '..', '..', '..', '..', 'resources', 'app', 'node_modules'));
module.paths.push(path.resolve(__dirname, '..', '..', '..', '..', 'resources', 'app.asar', 'node_modules'));

Ubuntu (electron . and build): Works with and without asar.
Windows (Build): Works with and without asar.
OSX: Untested, but should work.

There's probably a less messy approach, but this is the only thing I've found which works.

Hope this helps someone.

@bitwombat
Copy link
Author

So where is the request module actually, relative to your application, and which of those module.paths.push is the one finding it?

I think this information will help the devs.

@nmalcolm
Copy link

It depends on how I'm running the app. Without the first line electron . fails, without the third/fourth lines the actual build fails (without/with asar, respectively), and I believe without the second line the build fails when running it on the command line from a parent directory (e.g. ./app/app).

The request module is located (when built) at app/resources/app/node_modules/request/.

@Lyrillind
Copy link

After I package with electron-packager, I run packaged app it tells me "can't find module". But if I run as "electron ." it works perfectly. Without asar, "electron /path/to/packaged/app/source" works but directly run packaged app, it fails. Don't know why...

@skyksandr
Copy link

Same for me. Electron 0.37.2

@malept
Copy link
Member

malept commented Mar 17, 2016

Could the people who have this problem please do the following:

  • fork https://github.com/atom/electron-quick-start
  • make the minimal amount of changes to the fork to get the problem to occur and commit the changes
  • post the minimal testcase repository link here so someone in the community can see if they can reproduce the issue themselves.

@rodhoward
Copy link

I'm also getting this issue here is a very simple example of the issue:
https://github.com/rodhoward/electron-quick-start

npm -v = 3.3.12
node -v = v5.4.1
I'm running Linux mint 17.2 64-bit
and I'm trying to install it on a windows 8 vm

@zachzhao1984
Copy link

same error here, I'm using electron-packager 6.0 and eletron 0.37.5

@ManuelOverdijk
Copy link

A solution for me is to install the required node module in the same directory as the electron.js file.

@pushpak
Copy link

pushpak commented Apr 26, 2016

Here's what i did for electron 0.37.7 and electron-builder 3.11.0

  mainWindow.loadURL("file://" + __dirname + "/index.html");

  mainWindow.webContents.executeJavaScript(`
    var path = require('path');
    module.paths.push(path.resolve('node_modules'));
    module.paths.push(path.resolve('../node_modules'));
    module.paths.push(path.resolve(__dirname, '..', '..', 'electron', 'node_modules'));
    module.paths.push(path.resolve(__dirname, '..', '..', 'electron.asar', 'node_modules'));
    module.paths.push(path.resolve(__dirname, '..', '..', 'app', 'node_modules'));
    module.paths.push(path.resolve(__dirname, '..', '..', 'app.asar', 'node_modules'));
    path = undefined;
  `);

This works on all platforms.

@tenKinetic
Copy link

A note for potentially confused travellers. Ensure you're not using --ignore="something-that-will-match-a-module-folder-name-as-well-as-what-you-meant-to-ignore".

@MWalid
Copy link

MWalid commented Jun 4, 2016

I had the same issue, none of the suggestions mentioned above worked for me.

The solution was to package my app under a windows virtual machine running on my mac, and everything worked seamlessly.

@rodhoward
Copy link

Rather than argue about if it works or not could the electron-quick-start package be updated to include lodash and/or jquery so that we have at least one working example of imported libraries.

@malept
Copy link
Member

malept commented Jun 6, 2016

@rodhoward if you'd like to request that, you probably want to do so at that repository - you'll likely get a better answer there.

@ahallicks
Copy link

I now have this issue too. It has only happened with a recent module I've installed: @paulcbetts/system-idle-time. Before I added this everything was fine.

I extracted the created app.asar generated from running electron-packager and there is no folder @paulcbetts in the node_modules directory so I presume it is not being packaged for some reason?

I have checked and I've not got any ignores anywhere. My app runs perfectly fine from the command line before being packaged.

@leshow
Copy link

leshow commented Jun 30, 2016

adding a native module, lzma-native has created this issue for me as well.

@maxdow
Copy link

maxdow commented Jul 1, 2016

Same for me with electron-spell-check-provider

@leshow
Copy link

leshow commented Jul 4, 2016

for those having this issue with a native module, i've had some help and am making some headway on this issue.

it appears to be (at least, in my case) because of a dll required by the native module not being found after the application is packaged.

I created a folder, dropped the dll in it and added it to my PATH, the asar-packed application loads fine subsequently. This is obviously not a solution that will help redistributing the application, but a step further nonetheless.

@leshow
Copy link

leshow commented Jul 4, 2016

Update: including the dll in the base electron app dir was sufficient for electron to find it

app/
       electron.exe
       msvcp120.dll
       libEGL.dll
       ...
      PUT DLL HERE!
       resources/ 
                        app.asar
                        electron.asar

@malept
Copy link
Member

malept commented Jul 13, 2016

OK, I've gathered enough information about this issue. For a lot of people commenting, the problem lies in using the asar feature with native extensions. This is a known limitation with asar. To work around this, there are a few options:

@RilRil
Copy link

RilRil commented Jul 18, 2016

I have the same issue and not using asar didn't help ..

@malept
Copy link
Member

malept commented Jul 18, 2016

If #217 (comment) does not help your issue, please follow the instructions in #217 (comment). Otherwise, it's very difficult to figure out what your specific problem may be.

@RilRil
Copy link

RilRil commented Jul 19, 2016

I tried to make a simple project here. I just added ursa native module and it just doesnt wok : A dynamic link library (DLL) initialization routine failed .

@develar
Copy link
Contributor

develar commented Jul 19, 2016

@RilRil Are you going to use electron-packager directly or electron-builder, is your project answer to electron-userland/electron-builder#598 (comment) ?

If you still use electron-builder, "and it just doesn't wok" — on Surface or on your machine?

@malept
Copy link
Member

malept commented Jul 31, 2016

I'm not sure why you're using nightmare with packaging an Electron app, it looks like something that you'd only use for tests.

and how to use the extraResources option, any doc or tutorial would be helpful.

You mean extra-resource? Like the API docs say, you specify one or more paths to files that you wish to include, e.g., --extra-resource=/path/to/file1 --extra-resource=/path/to/file2. But that's outside the scope of this issue.

@jjTudu
Copy link

jjTudu commented Aug 1, 2016

i use nightmarejs to scrap the site, and the scraped data is used for calculation.
Algo.

  1. user select which data is required
  2. nightmarejs fetch the data from url.(nightmarejs support click event)
  3. the data i scrap is used for calculation.
    Note: data cannot be scrap without click, or change events.
    is my approach completely wrong? and it's not only nightmarejsthrow error, other external library gives too
    and Thank you for prompt reply

@malept
Copy link
Member

malept commented Aug 1, 2016

It doesn't feel right to have to run another Electron instance just to scrape data from a site. However, there seems to be problems with using a single Electron binary. But again, this is offtopic for this issue.

As I said earlier, a minimal testcase would be greatly appreciated.

@pilar1347
Copy link

I have an Electron app that I want to generate another Electron app. The idea is the user inputs images and text using the master app and then can press a button to generate an exe/app file to distribute.

I've created a test case to illustrate. I've only created this for darwin x64 right now. To recreate the issue clone repo and npm install in both the root folder and the app folder. Test in development mode using command npm start. Press the button in the app and you will see in the console log that the child app has been created successfully.

But if you build the master app using these parameters:
electron-packager app test --platform=darwin --arch=all --version=1.3.3 --overwrite --out=builds
Run that .app and press the generate button, the error Cannot find module 'electron-packager' appears. Any ideas why this is happening?

@malept
Copy link
Member

malept commented Aug 12, 2016

@pilar1347 this is a different error than the issue this one is about, could you please file a separate issue? (Making sure you read our docs on filing bugs and follow the directions in the GitHub issue template) It would be much appreciated.

@malept
Copy link
Member

malept commented Aug 12, 2016

I think I'm going to close this particular issue because there's already one major answer in here and there are way too many different kinds of problems to get a coherent answer for all of them.

IF YOU HAVE THIS PROBLEM

  • Read our docs on filing bugs and see if your specific problem already has another issue created for it (whether it's open or closed)
  • If you believe you have a different set of problems, file a new issue and follow the directions in the GitHub issue template.

@idododu
Copy link

idododu commented Oct 27, 2016

package.json won't work

{
    ...
    "devDependencies": {
        "jsonfile": "^2.4.0"
    }
    ...
}

package.json works

{
    ...
    "dependencies": {
        "jsonfile": "^2.4.0"
    }
    ...
}

it looks like that electron-packager looks up dependencies field to pack rather than devDependencies

@malept
Copy link
Member

malept commented Oct 27, 2016

@idododu

From the README:

Be careful not to include node_modules you don't want into your final app. If you put them in the devDependencies section of package.json, by default none of the modules related to those dependencies will be copied in the app bundles. (This behavior can be turned off with the --no-prune flag.)

@codyborn
Copy link

codyborn commented Nov 5, 2016

I found the proper solution was to migrate the dependency I wanted to be packaged with the application to a separate "dependencies" node in the packages.config. electron-packager by default will remove the modules listed in the devDependencies section.
Example:
"dependencies": { "electron-squirrel-startup": "^1.0.0" }, "devDependencies": { "electron": "^1.3.4", "electron-squirrel-startup": "^1.0.0", "electron-winstaller": "^2.3.4" }

Hope it helps!

@jasonkester
Copy link

jasonkester commented Nov 15, 2016

Strange that this got closed before a solution was posted, but codyborn above hits it:

By default, nothing in your node_modules folder will be copied to your final package.

You need to add a "dependencies" section to your package.json that explicitly lists everything in your node_modules folder. You also need to dig in to each of those dependencies and fish out a version number to add as well. Like this:

{
    "name": "your-app",
    "version": "0.1.0",
    "main": "main.js",
    "dependencies": {
        "aws-sdk": "2.6.9",
        "electron-json-storage": "2.0.3",
        "ipc": "0.0.1",
        "shell": "0.3.2"
    }
}

I have no idea why this is not mentioned anywhere in the packaging docs, as it seems to me to be the most important piece of information. As this issue shows, your app simply won't work if you don't do this.

@malept
Copy link
Member

malept commented Nov 15, 2016

By default, nothing in your node_modules folder will be copied to your final package.

This is not true. I'm not sure how you arrived at this conclusion. You contradict this statement in your next sentence.

I have no idea why this is not mentioned anywhere in the packaging docs, as it seems to me to be the most important piece of information.

It's implied in the example in the README:

Let's assume that you have made an app [...] and that the following is true:
[...]

  • npm install for the Foo Bar app has been run at least once

If you think this is not explicit enough, I am happy to review pull requests to make it more obvious.

@malept malept changed the title "Uncaught Error: Cannot find module" after packaging for win32 "Uncaught Error: Cannot find module" when using asar with native modules Nov 15, 2016
@dougludlow
Copy link

I ran into this issue while rebuilding edge.js for electron. Ended up that I was missing the vcruntime140.dll.

See Error: The specified module could not be found for more details. Thanks for the hint @leshow!

@tylervick
Copy link

Had to do this to build Hain as well - surprising we don't have a better solution for this...

@MyNameIsAlaa
Copy link

@jasonkester thanks man... you saved my day!

@SuperStewDK
Copy link

I solved this issue by adding the dependency to the configuration file.

@ashwinmr
Copy link

I figured out the reason for my issue and the solution.
I was installing the module with the --save-dev flag. This was making it a development dependency and it would work when the application was started using electron .. But it wouldn't worked when packaged into an application.
The fix was to install the module without the flag. This would make it a normal dependency.
This automatically configures the package.json correctly.
Hope it helps !

@rip3rs
Copy link

rip3rs commented Oct 16, 2018

I encountered the same problem.

But I realised I was using gulp browserify and sending my app to dist/main.min.js.... then have electron.js etc on the root folder.

With this bundled, electron cannot find node_modules, because it does not exist. be careful of this.

@xthdraft
Copy link

Following on from codyborn's and jasonkester's comments above, I think the docs on electron-builder and electron-packager should explicitly advise that things in node_modules that are not added to the dependencies field in package.json will not necessarily end up in the asar file.

@super9719
Copy link

am getting the same err : "can't find module mysql2", am using --ignore=node_modules to ignore node_modules and that's why am getting this err, is there a way to fix this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question ❓ Question about using Electron Packager. Not supported in this issue tracker.
Projects
None yet
Development

No branches or pull requests