fyears / electron-python-example

Electron as GUI of Python Applications
MIT License
2k stars 234 forks source link

Module mismatch following readme instructions #23

Open abulka opened 6 years ago

abulka commented 6 years ago

Following the recommended readme instructions, where package.json contains:

  "devDependencies": {
    "electron": "^1.7.6",
    "electron-packager": "^9.0.1"
  }

results in a javascript error

The module '/Users/Me/Devel/electron-python-example/node_modules/zeromq/build/Release/zmq.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 54. This version of Node.js requires
NODE_MODULE_VERSION 57. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.module.(anonymous function) [as dlopen] 

because specifying "electron": "^1.7.6", these days (2018) will cause npm to actually pick up electron 1.8.4 - which won't work with your custom zerorpc version.

This is because the ^ symbol in package.json means the latest available 1.* version of electron is searched for and installed, see npm versioning.

I finally got the example to work by removing the ^

  "devDependencies": {
    "electron": "1.7.6",
    "electron-packager": "9.0.1"
  }

thus causing that exact version of electron to be installed, which matches your 'prebuilt' zerorpc dependency "zerorpc": "git+https://github.com/fyears/zerorpc-node.git".

I believe that the readme should be updated to remove the ^ from package.json to help users avoid this problem.

abulka commented 6 years ago

Turns out that the module mismatch problem can be fixed by installing electron-rebuild

npm install --save-dev electron-rebuild

and running it after npm install. This step's purpose is to rebuild native Node.js modules against the version of Node.js that your Electron project is using. Remember that electron comes with its own internal version of node. The rebuild step will rebuild the node module zerorpc from fyear's custom repository to work with whatever version of electron happens to get installed when you npm install.

# install dependencies  based on the package.json first
npm install

#every time you run 'npm install',you have to run this:
./node_modules/.bin/electron-rebuild

# run
./node_modules/.bin/electron .

This means we don't need to remove the ^ from package.json. Whatever version of electron is installed, the rebuild step will repair any module mismatch issues.

jafermarq commented 6 years ago

@abulka, thanks for the above. Did you manage to pack the application? I manage to compile it ok, but still, have the same error when running the generated executable(MODULE_VERSION...). I tried adding the argument --electron-version=1.7.6 to electron-packager but didn't solve it.

I run this bash script to do the whole thing at once:

rm -rf ~/.node-gyp
rm -rf ~/.electron-gyp
rm -rf ./node_modules

npm install --save-dev electron-rebuild

# install dependencies  based on the package.json first
npm install --runtime=electron --target=1.7.6

#every time you run 'npm install',you have to run this:
./node_modules/.bin/electron-rebuild

# run (works great)
./node_modules/.bin/electron .

# Build python app
pyinstaller pycalc/api.py --distpath pycalcdist

rm -rf build/
rm -rf api.spec

# build Electron app
./node_modules/.bin/electron-packager .  --electron-version=1.7.6 --overwrite --ignore="pycalc$" --ignore="\.venv" --ignore="old-post-backup"
yeshymanoharan commented 6 years ago

I am also running into the error @jafermarq pointed out. Specifically, I'm getting the error cannot find module 'zerorpc' in the chromium console once i try to launch the app. I'm thinking that electron-rebuild has to be integrated with electron-packager, which is documented on the electron-rebuild github page.

yeshymanoharan commented 6 years ago

ok @jafermarq I found the error.

When you use the electron packager, change the '--electron-version' tag to the version of electron that packages are being rebuild to (1.8.4). And make sure that all of the node modules are being included by using the '--no-prune' flag.