node.jsnode.js-addonnode-addon-api

Global shared data between different instances of a Node.js native addon?


I'm looking to create a native addon with Node-API (more specifically with the C++ wrapper module node-addon-api).

I need to have global read-only data that will be loaded once and will be shared between different instances of the addon (e.g. when the addon is loaded by different node.js workers).

Is there a recommended way to do this? Would it be safe to just store the data in a static variable?


Solution

  • It depends on the type of your data. If your data does not involve any V8 data types (or Napi:: types), then, yes, it is generally safe.

    Just be careful to not shared a variable that has been initialized with Node::Env - this is the rule to follow - if it needs Node::Env then it can't be shared.