0

How would I write nodejs addon which would support all versions (atleast all stable versions > 0.10.6) of nodejs. For example, one version would have String::Utf8Value name(args[0]); where as another would have node::Utf8Value name(args[0]);. This is just an example but I have many scenarios where there will be different code for different versions of nodejs. As far as I know this could be achieved in following ways.

  1. Defining pre-processor to check which version and compile code accordingly.

if defined()//Not sure what exactly have to checked

include <nameser.h>

else

include <arpa/nameser.h>

endif

If this is the best option(which I don't think) even if multiple places pre-processor as to be added and code looks ugly, how would I achieve this. Meaning how would I check which version of NodeJS inside C/CPP addon.

  1. Having separate file for each version and defining conditions inside binding.gyp to compile specific file based on nodejs version. If this is the best option, which variable I can refer to check the nodejs version.
  2. Having tags while publishing npm package so that user can install for his specific nodejs version. Tag a published version. Although user has to check for the nodejs, non technical person won't be executing this, so shouldn't be a problem. The only problem I am seeing with this approach is versioning. Example, If there is a fix which has to applied for multiple tags, then, for every tag publish I have to specify different version (Not sure though).

Any other way I can achieve this which I am not aware of, if any the above options is not the good option to go with ?

Royal Pinto
  • 2,869
  • 8
  • 27
  • 39
  • 3
    Another possibility would be to use [`nan`](https://github.com/rvagg/nan) -- Native Abstractions for Node.js. "*A header file filled with macro and utility goodness for making add-on development for Node.js easier across versions 0.8, 0.10 and 0.12 as well as io.js.*" – Jonathan Lonowski Apr 07 '15 at 17:24
  • @JonathanLonowski Great tool. But my add-on is almost finished and to implement with this requires major code change so wouldn't go with this. I will definitely try using this for next version of my add-on. – Royal Pinto Apr 08 '15 at 13:57
  • Another possibility would be having separate file for each version (which is actually the case in my addon) and having pre-build script to chose appropriate file based on the `node` version. Similar to `Option 2` mentioned in the question but instead of having conditions in `binding.gyp` having separate pre-build script. – Royal Pinto Apr 08 '15 at 14:02
  • I realized that `Option 2` is possible. In binding.gyp configuration, shell commands can be executed and output can be captured. This output can be compared to compile version specific files. Sample command: `"<!(node -e \"var semver=require(\'semver\'); console.log(semver.gte(process.version, \'0.12.0\'));\")"` – Royal Pinto Apr 13 '15 at 06:11
  • @JonathanLonowski though `option 2` is possible to implement its very difficult to maintain multiple versions of files. I changed my mind. Going to use the tool you suggested(nan), thanks. – Royal Pinto Apr 13 '15 at 06:21

0 Answers0