Electron-packager: "Uncaught Error: Cannot find module" when using asar with native modules

Created on 13 Dec 2015  Â·  55Comments  Â·  Source: electron/electron-packager

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

question

Most helpful comment

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...

All 55 comments

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.

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.

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

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.

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.

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/.

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...

Same for me. Electron 0.37.2

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.

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

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

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

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.

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".

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.

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.

@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.

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.

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

Same for me with electron-spell-check-provider

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.

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

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:

  • Use asar.unpack and asar.unpackDir. The Electron packaging guide suggests doing --asar.unpack="*.node", but you may need to do some experimentation to make the asar options work for your specific use case.
  • do what @leshow suggests
  • don't use asar (although there have been at least two issues filed arguing that asar should be on by default).

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

If https://github.com/electron-userland/electron-packager/issues/217#issuecomment-232376957 does not help your issue, please follow the instructions in https://github.com/electron-userland/electron-packager/issues/217#issuecomment-197969870. Otherwise, it's very difficult to figure out what your specific problem may be.

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 .

@RilRil Are you going to use electron-packager directly or electron-builder, is your project answer to https://github.com/electron-userland/electron-builder/issues/598#issuecomment-233408351 ?

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

I'm supposed to use electron-builder, but for now even 'electron index.js' doesn't work on the sample project on my machine..

back to my problem, the thing that i dont understand is that the module not found (%userprofile%\AppData\Local\Temp\%randomname%.tmp.node) exists ...
and why is it random name ?

Ok I made some progress,
I run the app a first time, I get the error...
then I go to C:\Users\AppData\ and I delete \Local and Roaming
I try again and it works !

I run my tests on a "clean" machine. Fresh windows install, no nodeJs, no electron, nothing.

Does this make sens to anyone ?

ok actually i've done another thing for it to work, I've installed Git for windows and I checked the "Use git and optional Unix tools for the windows Command Prompt" option ..

so, it looks like the packager or the builder is using a command which does not exist on Windows ...

edit: it comes from Mingw

Git shouldn't be necessary unless your package.json file has a git reference in dependencies or devDependencies.

If an executable is called from Node and scnnot be found, there should be an ENOENT exception thrown, so I'm confused as to why that didn't happen in your case.

okay actually a .dll was missing that Git adds when installing with the option! It was libeay32.dll (SSL).
So the solution from @leshow works, I just added the missing .dll next to the .exe and it worked.

This sounds like a problem with one of your dependencies - it should be bundling that DLL with a binary Node module, and then any of the solutions from my 7/13 comment should work.

Same problem here...
Make an installer is harder than making a project....
There should be an easy workaround

Please note that adding comments that say "same here" or "+1" or similar are not particularly helpful and just sends unnecessary notifications to everyone subscribed to the issue.

What would be more helpful is providing a minimal testcase to reproduce your problem. Using the electron-quick-start repository as a base, create a minimal Electron app that illustrates the issue you described, and post a link to it here. This makes it easier for the community to figure out what the specific cause might be.

i have external modules in project,
npm i nightmare --save

using through require('external modules'), in development mode it works fine, but after electron-builder process, it throw error in module.js 440 Cannot find module.

And yes require is used in render processes. what i guess is external modules are not being added in electron.asar file, but how to add? and how to use the extraResources option, any doc or tutorial would be helpful.

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.

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

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.

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?

@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.

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.

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

@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.)

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!

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.

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.

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!

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

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

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

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 !

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.

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.

Was this page helpful?
0 / 5 - 0 ratings