Can you pack multiple native modules in one NPM package?

I wrote a cross-platform nodejs native module in C using cmake , musl , as well as a few abstractions and carefully placed # ifdefs .

It is integrated into the module, using the cmake-js , and I have 3 build server: Windows, Macand Linuxmaking buliding, so I have 3 of their own modules that work well on all platforms.

We have an internal NPM repository that I could publish so that other teams within the company could just npm install moduleget my module.

The fact is that it is dirty, that the client will need to select the module on the platform on which they are located (i.e. I would deploy all 3 modules from each of the build servers), so I would prefer to deploy one module somehow combined 3 platform-specific modules.

Is this possible and how can I structure the internals of such a module? *

Are there bindings , for example, to determine the platform and find the corresponding file .node?

& ast; I don’t want to include the code and compile it, because it puts a rather heavy load on the client (they would expect that they could write a simple script using Javascript, for example, and use my module - it would be a shock that they need to configure C assembly chains, worry about the platform on which their script is running, etc. etc. etc.).

+4
1

, - , , :

  • fancy-module-linux
  • fancy-module-windows
  • fancy-module-mac

, fancy-module.

:

fancy-module - README

install script package.json

{
  ...
  "scripts": {
     "install": "node ./install_platform_dep.js"
  }
}

fancy-module install_platform_dep.js script. install_platform_dep.js :

// For Windows...
if(process.platform === 'win32') {
    // Trigger Windows module installation
    exec('npm install fancy-module-windows', (err, stdout, stderr) => {
         // Some error handling...
    }
} else if ... // Process other OS'es

! fancy-module:

package.json
README.md
src
  |- Windows
  |  # Here you've got just copied contents of `fancy-module-windows`
  |- Linux
  |  # This folder have same contents as `fancy-module-linux`
  \- Mac
     # Same

exec('npm install ./src/Windows' /* ... */)

.

, , .

, , - , , afik - .

( Electron paltform) npm - , .

Edit:

os , : is-linux is-windows .., process.platform

is-linux
is-windows
process.paltform
hook .json

# 2:

node-pre-gyp, :

- node -pre-gyp

- .

+2

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


All Articles