2

My nodejs app runs on Raspberry Pi and uses a SPI interface. SPI is not available on windows.

The npm package I am using fails to install on windows as I would expect. I would like to develop and do some testing on Windows but NPM failing to load this module forces too many manual workarounds.

What I would like the ability to install the SPI module when NPM install is ran on a Raspberry and install a stub module to fake a SPI on windows.

In pseudo code : file package.json

"dependencies-for-linux": { "pi-spi":  "~1.0.1"},
"dependencies-for-win": { "pi-spi": "./some_local_file_to_fake_pi_spi_api"}

An alternative approach could be to not abort the install is the package fails. Pseudo code for the alternative: file package.jon

"dependencies-that-do-not-fail-install" : { "pi-spi":  "~1.0.1"}

my_module.js

 var pi_spi = null
    try{
        pi_spi = require("pi-spi")
    }
    catch (e) {
        pi_spi = require("fake-pi-spi")

Either solution would be great. Thank you in advance for your help.

grabbag
  • 810
  • 11
  • 28
  • You could make a node script that you run at `postinstall` where you check the `platform` and programatically `npm install` (via `require('npm')`) – topheman Jun 24 '16 at 20:32

1 Answers1

0

You can use the OS module in Node.js to get the current os.arch() which will return either 'x64', 'arm', etc...

You can use this in my_module.js to see which package to require.

https://nodejs.org/api/os.html

  • How do i use os.arch to alter the npm installation of packages? I see os.arch can in the js code require an alternative package. But the package that fails to install will mess up the installation before I get this far. So i think I need to be able to control what gets installed. – grabbag Jun 24 '16 at 20:57
  • In that case you when you run npm install in windows you can specify the --only=dev flag. Then you can change your "dependencies-for-win" to devDependencies for the fake-pi-spi. See more info here [link](http://stackoverflow.com/questions/9268259/how-do-you-prevent-install-of-devdependencies-npm-modules-for-node-js-package?rq=1) – JayhoPeter Jun 24 '16 at 21:17
  • 1
    Thanks, the dots are now connected. optionalDependencies allows for the spi to fail when on windows, try catch on the require("pi-spi"). – grabbag Jun 24 '16 at 21:35