📜 ⬆️ ⬇️

Ready native modules for node.js

If you are not new to nodejs, you most likely know that one of the advantages of nodejs is the ability to write native modules . They are usually used when some low-level access to the system is needed. The developer of native modules faces a number of problems associated with porting, testing, and also distributing code. It is at the last I would like to focus.


It seems that everything is simple: I wrote a module, made js a wrapper, tests and publish it. As if not so. Unlike standard js modules, to install natives, the build environment is gcc / visual studio / etc. And if everything is simple with linux distributions, on windows systems not every script writer has an installed c ++ compiler. In addition, each module that yours will have in dependencies will also require an assembly environment for installation. So how to be?


I think almost everyone uses travis / appveyor to test their code. Only native modules pass the build cycle before testing. It would not be bad when publishing a package to take all collected binaries from CI. Fortunately, other developers thought the same way and created such a system. It consists of three modules: prebuild helps to compile modules, prebuild-ci takes ready assembled modules from CI, if in the last commit there was a change in the package version, prebuild-install helps to install such assembled modules.


Setting up this whole system is very simple. First, you need to configure the assembly. prebuild is responsible for this, it downloads the correct version of the source nodejs and compiles the module in the folder ./prebuild . One thing is important here. In order for the installation of dependencies you do not start compiling using node-gyp , you need to disable the initialization scripts.


 yarn install --ignore-scripts #  npm i --ignore-scripts 

After that, write the prebuild build prebuild --strip and that's it! I add the key --strip , for sure to remove all unnecessary information from the binary. Next, the test build starts and here prebuild-ci will help us. Include it in your testing pipeline and will understand from itself when it is necessary to pick up and fill in the modules:


 ava test/*.js && prebuild-ci 

For prebuild-ci work correctly, the env variable PREBUILD_TOKEN with your github token must be set in CI. This is necessary for the possibility of creating github release, as well as loading the compiled binaries into this release. Tokens are specified at https://github.com/settings/tokens . Click on Generate new token , set the name and click on Generate token . I recommend setting the rights to repo , although the authors write that the default should suffice. Read more about tokens in the original readme .


After that, the next time you make a release, all modules assembled in CI will go to github releases. I strongly recommend waiting for the completion of all tests and the assembly of all modules before publishing the package to npm. By the way, if you use np to publish packages, there is a corresponding issue .


At the end you need to tell the package manager how to install the modules. For this, there are npm scripts and the install stage. It works after installing the module. The package manager will launch prebuild-install , and he in turn will go for ready-made binaries. Nevertheless, it is recommended to set the node-gyp rebuild at the end, so that users for whom there is no ready-made package could assemble the module in the standard way.


 "scripts": { "install": "prebuild-install || node-gyp rebuild" } 

You can connect the finished module using bindings . Now, by taking these few simple steps, your users will thank you. You can see how this system looks all together in node-process-list or leveldown .


')

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


All Articles