📜 ⬆️ ⬇️

SQLite and NW.js - step by step instructions for creating a strong friendship

Hi, Habr and its inhabitants! I wanted to sculpt something on HTML and JS, so that it was desktop and convenient. What is needed for this? Right. Node.js and NW.js or Electron. Having looked at the weak attempts of holivar NW vs Electron and after reading the documentation of both of them, it was decided to start to touch NW.js.

But here's an ambush. Running npm i sqlite3 installs only those libraries that can be executed during a direct launch through node. And if you try to connect the script in html and run this html in NW.js, then nothing will come of it. Google associates give out only one recipe for including sqlite in a project on nw.js, and that one has to be pulled out of the cache. Yes, and it is outdated. Therefore, it took a couple of days to study the problems and find a working solution. I ask under the cat.

So. For a start, a machine “from what it was” was assembled and Windows 7 x32 with .NET 4.7.1 package installed (required for Visual C ++). Why 32 bits? I decided to start small. Although, as it turned out, the creation of a module for x64 does not require execution on the x64 system.

Then Node.js version 10.8.0 was delivered. Although at the end of the experiments 10.9.0 had already appeared. But then I decided to listen to knowledgeable people:
')


After deliberation, I decided that I was still doing for the LTS, although it is not entirely clear where all of them have LTS, and where not. Assumed that non-LTS - is beta, alpha and nightly build.

Although, like any dropout, I did everything in half and did not fulfill the following recommendation:

Recommendation from the documentation for LTS-releases
On Windows, you need to replace the file
<npm-path> \ node_modules \ node-gyp \ src \ win_delay_load_hook.cc

Well, I did not find this file on my computer. Probably because I did not install node-gyp.

For the node set (it is imperative to do from the command line with administrator rights)

npm i -g nw-gyp

In fact, the replacement node-gyp
NW-GYP to support NW.js specific headers and libraries.

Next - download Visual Studio Community Edition (the current link is easily found in your favorite search engine) - at the time of this writing, the current version is 2017. What is important for us - it contains Build Tools 2015 - while the native modules in the node are only assembled with this version and more fresh do not support. Download the installer, run and put one tick in the tab “Individual Components” - VC ++ 2015.3 v14.00 Toolkit (v140) for PC



The new Windows 8.1 SDK is also required, so we install all three items. Installation in this form leads to a jump of 2.89 gigabytes of all necessary.

Going further - swing the snake. Moreover, version 2.7. Set by default in C: \ Python27.
And now - attention! Ambush.

The default installer assumes that it is not necessary to add the location of the python in PATH, therefore we eliminate this misunderstanding during the installation phase.



A similar ambush haunts many developers when building a wide variety of modules for a node. And all because the small-scale corporation also does not consider it necessary to prescribe the path to their creations. The githab is simply overwhelmed with assembly errors due to the fact that after installation, the path to the assembly tools is not included in the PATH. We also correct this misunderstanding - we correct the environment variable so that it allows the assembly to go through, if not very smoothly, then at least without errors. That's what happened with me (broken into lines for better perception)

>echo %PATH%
C:\Python27\;
C:\Python27\Scripts;
C:\Windows\system32;
C:\Windows;
C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\Program Files\nodejs\;
C:\Users\Den\AppData\Roaming\npm;
C:\Program Files\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin;
C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\v140;


Bold is what you need to add manually. Naturally, making sure that these paths exist.

Required item - reboot .

Well, now the actual assembly. I used the manager FAR, but no one forbids the old school cmd

c:\
md app
cd \app
npm init
npm install sqlite3 --build-from-source --runtime=node-webkit --target_arch=ia32 --target=0.32.1 --msvs_version=2015

for x64, change --target_arch = x64.

Well, that's all. If done correctly, the build runs with a number of messages like

..\src\database.cc(672): warning C4996: 'Nan::MakeCallback': deprecate [C:\app\node_modules\sqlite3\build\node_sqlite3.vcxproj]
C:\app\node_modules\nan\nan.h(929): note: . "Nan::MakeCallback"
c:\app\node_modules\nan\nan_new.h(208): warning C4244: : "sqlite3_int64" "double", ( ..\src\database.cc) [C:\app\node_modules\sqlite3\build\node_sqlite3.vcxproj]

but in the end the module is assembled and located in
C:/app/node_modules/sqlite3/lib/binding/node-webkit-v0.32.1-win32-ia32\node_sqlite3.node

The nuance of the assembly - with each assembly the node_modules / sqlite3 / lib folder is deleted by the collector. Therefore, if there is a need to assemble two modules (for each architecture), save the intermediate result.

Thank you for your attention, finally the result of the experiment:

Assembled modules for NW.js 0.32.1 ia32 && x64 on googledisk

Source: https://habr.com/ru/post/420413/


All Articles