My Electron app uses a nodejs dependency (in my case it's mysqljs) and it works fine when running on development mode.
However when I build it and launch as a native desktop app, it fails with:
Electron ERROR Error: Cannot find module 'mysql' Require stack: ...
The problem was that I accidentally placed mysql between the devDependencies of my package.json file.
It should instead be placed within the dependencies since it is supposed to be shipped with the app.
Related
Current behavior
I know this kind of issue was fixed in the version 5.1.0 but ...
I'm trying to seed DB in the Node JS part logic at the before hook
We have the cls-hooked and 1 more internal npm module for sharing both use async_hooks
When I'm trying to import and use any of that modules I receive crashes at the test script launch:
Error: Webpack Compilation Error
./node_modules/...[our private module name].../lib/...[file name]....js
Module not found: Error: Can't resolve 'async_hooks' in '/Users/.../node_modules/...[private module or cls-hooked].../lib'
resolve 'async_hooks' in '/Users/.../node_modules/...[private module or cls-hooked].../lib'
Parsed request is a module
using description file: /Users/.../node_modules/...[private module or cls-hooked].../package.json (relative path: ./lib)
Field 'browser' doesn't contain a valid alias configuration
resolve as module
Desired behavior
Test script launch should go smoothly with before hook executing
Test code to reproduce
require some module in ./cypress/plugins/index.ts with next similar logic
var async_hooks = require("async_hooks");
var asyncLocalStorage = new async_hooks.AsyncLocalStorage();
...
asyncLocalStorage.getStore();
Cypress Version
^8.1.0 - 8.5.0
Cypress binary version: 8.5.0
Electron version: 13.2.0
Bundled Node version: 14.16.0
tried Node version: 14.17.6 and 16.9.1
the same for:
Cypress binary version: 7.7.0
Electron version: 12.0.0-beta.14
Bundled Node version: 14.15.1
Other
additional install of "async_hooks": "^1.0.0" didn't help
Using TS
"typescript": "^4.1.5"
OS
macOS Big Sur 11.16
The problem was in 1 common file for both processes browser and NodeJs in Cypress.
In this case, Webpack built the bundle with 'browser' context but received NodeJs context logic. That's why the error occured.
When I used enum in Browser part in the file which besides that also exports NodeJs logic it (Webpack) didn't make the 'treeshaking', thus the NodeJs logic was imported into the bundle for browser process logic. 🤷♂️
I am using electron 11.1.0 and electron-builder 22.10.5
I created a installer for my electron app which is using native module using electron-builder and its working fine on my laptop but on my friends laptop I am getting error
A JavaScript error occurred in the main process
Uncaught Exception:
Error: The specified module could not be found.
\\?C:\some\path\My-Electron-App\resources\app.asar.unpacked\node_modules\obs-studio-node\obs_studio_client.node
I have checked C:\some\path\My-Electron-App\resources\app.asar.unpacked\node_modules\obs-studio-node\obs_studio_client.node file exists.
I guess its asar related issue with native module. I tried by adding "asarUnpack": ["**/*.node"] in my build config but does not help.
How can I fix this problem?
I could not get this working with asarUnpack. Instead I use
"asar": false
Increases the bundle size, but at least it works.
I'm creating vue application with nuxt.js (universal mode).
nuxt build command runs correctly and create .nuxt folder in project.
i dont want run it locally because nuxt start is ok. i need run on a window server with urlRewrite & iisnode.
when i run server.js in .nuxt served folder with node server.js, the exception accured:
same as node .nuxt/server.js in local
import { stringify } from 'querystring'
^
SyntaxError: Unexpected token {
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
...
I know the problem is because of esm module import/export that doesn't support by node.js. i want transpile into commonJs when building project. (require and module.exports).
for example:
server.js in .nuxt folder (created with build)
must be:
node: 10.15.3
npm: 6.8.0
vue-cli: 3.8.2
i did some ways with nuxt.config.js build option configuration and doesnt resolved.
how can i do that? What's wrong?
example on codesandbox
This is similar to this Electron Uncaught Error: A dynamic link library (DLL) initialization routine failed
ELECTRON_ASAR.js:173
return old.apply(this, arguments)
^
Error: A dynamic link library (DLL) initialization routine failed.
\\?\C:\workspace\client\client\desktop\node_modules\sqlite3\lib\binding\node-v57
-win32-x64\node_sqlite3.node
at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:173:20)
at Object.Module._extensions..node (module.js:671:18)
at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:173:20)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:504:12)
at Function.Module._load (module.js:496:3)
at Module.require (module.js:586:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\workspace\client\client\desktop\node_modules\sqlit
e3\lib\sqlite3.js:4:15)
at Object.<anonymous> (C:\workspace\client\client\desktop\node_modules\sqlit
e3\lib\sqlite3.js:190:3)
I've tried everything suggested in this post and anything else I can find
I've installed electron-rebuild and added "rebuild": "electron-rebuild -f -w sqlite3 -V 1.7"
This is running without errors.
I tried "postinstall": "install-app-deps",
This isn't helping..
Anything else to try?
The files are in there..
The
This ended up being a mashup of a bunch of different issues on my end. I'll start with the simplest and go from there.
Platform
Your error there indicates that it wants node-v57-win32-x64, but if you are running from Electron it should be electron-v2.0-win32-x64 (or electron-v1.7-win32-x64 if you are using an older version). I ran into this as well because I am running sqlite3 in a child process. Child processes in Electron are in a full node context and not an Electron (browser or node+browser) context. I fixed this by passing the Electron version from the main browser process (process.versions.electron) to the child process via an environment variable and setting it on process.versions.electron in the child process.
WARNING: that is a HACK and works because I'm not using anything that expects a full Electron environment in that process. It is literally only to make it find the correct native bindings. A more correct fix would be to look into making electron-builder build node-v57-win32-x64 instead.
Speaking of which, let's create those next.
postinstall
"scripts": {
...
"postinstall": "electron-builder install-app-deps"
}
This should attempt to rebuild your native dependencies when running npm install or yarn install. If you have a project/app directory, they will be placed in project/app/node_modules/. Otherwise they'll go in project/node_modules.
Transitive Dependency
In my case, sqlite3 is actually a transitive dependency of another package in my dependency set. While electron-builder on Linux/OS X picked these up correctly and showed this during installer creation:
• rebuilding native production dependencies platform=linux arch=x64
• rebuilding native dependency name=sqlite3
the Windows build showed:
• no native production dependencies
This was fixed by adding the transitive dependency as a direct dependency. After I did this, the package started to be picked up by electron-builder for compilation and was spitting out errors.
Windows Build
To fix those errors:
Install python (I used the latest 2.7)
Install Microsoft Build Tools 2013 and restart
run your postinstall script again
This is the point where I said it was "half-solved" on my end. This should be the full solution for basic electron project setups.
yarn workspaces
I use the yarn workspaces feature and my project setup is something like this:
yarn-workspace-project/
workspace/
project/
web-app/
project-electron/
app/
For this kind of setup, yarn install is generally run in the yarn-workspace-project or yarn-workspace-project/workspace directory, rather than in each project. This produces a hoisted node_modules/ directory at yarn-workspace-project/node_modules. When creating the packaged version, electron-builder grabs the dependencies from that hoisted location and everything runs.
When running my start script in project-electron, however, electron . had some issues finding the native binding. The package using sqlite3 was installed under yarn-workspace-project/node_modules, and was therefore resolving sqlite3 to yarn-workspace-project/node_modules/sqlite3. This definitely exists per the yarn install, but the electron bindings got put in yarn-workspace-project/workspace/project-electron/app/node_modules/sqlite3.
There are multiple obvious solutions to that, so I'll leave that as an exercise to the reader. The key problem here is that the default bindings that get downloaded (I think? I don't recall them being built) for node-v57-win32-x64 do not work out of the box for Windows 10 like they do for Linux / OS X, even though the error indicates that they don't exist (which they do).
I have created an electron (windows) app with electron-prebuilt, native node modules and python server. I want to include other node modules like serial-port and web-scraper with my app. I have installed the modules with npm and the modules get bundled in "app.asar" file. But on using the module with require method in my javascript function throwing "cannot find the module" error. What to do?