Saturday, July 21, 2018

Using native Nodejs Modules in Electronjs in Windows


First of All prepare package.json file to do the job and add the following script option:

rebuild": "electron-rebuild -f -w  sqlite3 

Here We are taking sqlite3 as an Example for the native Nodejs Module that we want to use it with electron App.
Install electron-rebuild :

npm install --save-dev electron-rebuild

Now,before you can compile node module to serve electron app you have to run the command :

npm install --global --production windows-build-tools

This command will install Python2.7 (Python 3 is not supported) and windows build tools . you may get this error :

events.js:183   throw er; // Unhandled 'error' event
Error: spawn powershell.exe ENOENT.

This is because the npm cannot run Powershell Command ,So to solve this error Add PowerSell Folder to the environment system variable Path:
%SystemRoot%\system32\WindowsPowerShell\v1.0

Then restart windows.

Go to this folder on your system : C:\Program Files (x86)\MSBuild

And find what folder available their.  if you have folder (14) and this folder has (Bin) folder with a file called MSBuild.exe in it , then you can use MS version 2015.
If there is a folder called (15) that has (Bin) folder with a file called MSBuild.exe in it , then you can use MS version 2017. let say you have the folder (14).then run this command :

npm config set msvs_version 2015

if you didn't choose the right msvs version you may get a dump Error which has code and errno :

npm ERR! code ELIFECYCLE
npm ERR! errno 4294967295

or ,if the version is not the right one. you may get the error :

 MSBUILD : error MSB4132 

Also Expected error is :

gyp ERR! stack Error: Can't find Python executable , you can set the PYTHON env variable
 
but this is a clear error and will disappear only after success of this command :

 npm install --global --production windows-build-tools
 

And by Adding the Python Folder to the Path variable that you have to do it by your self.