Implementing a Plugin Manager in NodeJS Application for dynamic extension at application level

Viewed 253

For a NodeJS server application i need to implement dynamic extendability with a plugin-like system. Goal is to be able, to hook in extended calculations by simple plugins, which select when and how to run by events they register for at the main app.

  • Within a folder "./plugins" each plugin should live in a subfolder.
  • The plugins should be loaded by the application automatically at startup.
  • No change at the application code is allowed for introducing a new plugin.
  • Plugins should be able to register for events in main application.
  • Communication between the plugins should be possible in some way

Is there any ready to use framwork for this requirement or are there any rock solid architecture approaches for this?

Regards

Boxson

1 Answers

Found a solution after some try and error:

  • Read the plugins directory (fs) and scan for subfolders
  • Check if there is a manifest.json file inside the subfolder
  • if manifest.json exists, read this file and parse content as JSON object
  • push the JSON object into an array (plugin list array)
  • iterate over the plugin list array and require the plugin main.js file
  • plugins register for events of the plugin manager object (made accessable via global.pluginmanager = pluginmanager

With appropriate error handling this seems to work solid.

Related