• Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
v3.5.2 Latest
Buy Hosting

Is it possible to require code from another plugin in your plugin?

Scheduled Pinned Locked Moved NodeBB Plugins
10 Posts 5 Posters 3.5k Views
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jongarrisonJ Offline
    jongarrisonJ Offline
    jongarrison Plugin & Theme Dev
    wrote on last edited by
    #1

    I'd like to call functionality from an existing plugin from my plugin. Is there a way to refer to the code in other plugins with something similar to this:

    require.main.require('./src/plugins/nodebb-plugin-name/lib/filename.js');

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #2

    Yes it should work as long as the required plugin is installed. The path would be something like

    require.main.require('./node_modules/nodebb-plugin-name/lib/filename.js');

    1 Reply Last reply
    2
  • jongarrisonJ Offline
    jongarrisonJ Offline
    jongarrison Plugin & Theme Dev
    wrote on last edited by
    #3

    Thank you @baris !

    jongarrisonJ 1 Reply Last reply
    0
  • jongarrisonJ Offline
    jongarrisonJ Offline
    jongarrison Plugin & Theme Dev
    replied to jongarrison on last edited by
    #4

    lol, of course it would be the filesystem path. Years of java has damaged me 🙂

    1 Reply Last reply
    0
  • frissdiegurkeF Offline
    frissdiegurkeF Offline
    frissdiegurke Plugin & Theme Dev
    wrote on last edited by
    #5

    I think it's better to use require('nodebb-plugin-name/lib/filename.js') (see node.js docs) rather than require.main.require('./node_modules/...') in this case 😉
    Try avoiding the use of node_modules anywhere in your code 😛

    jongarrisonJ 1 Reply Last reply
    3
  • jongarrisonJ Offline
    jongarrisonJ Offline
    jongarrison Plugin & Theme Dev
    replied to frissdiegurke on last edited by jongarrison
    #6

    @frissdiegurke Thanks! That makes sense and works well with one caveat. It looks like from a plugin that you need to refer to the main module loader (is that the right terminology?) like so:

    require.main.require('nodebb-plugin-pluginname/lib/modulename');

    julianJ 1 Reply Last reply
    0
  • julianJ Offline
    julianJ Offline
    julian GNU/Linux
    replied to jongarrison on last edited by
    #7

    @jongarrison Yes, you'd need the require.main to require from the NodeBB context.

    1 Reply Last reply
    1
  • yariplusY Offline
    yariplusY Offline
    yariplus Community Rep
    wrote on last edited by
    #8

    I've used plugins.libraries['pluginid'].function() to do this. Are there any caveats to using this method? Other than the plugin needing to actually be enabled.

    frissdiegurkeF 1 Reply Last reply
    1
  • frissdiegurkeF Offline
    frissdiegurkeF Offline
    frissdiegurke Plugin & Theme Dev
    replied to yariplus on last edited by
    #9

    @yariplus The plugin needs to export the method you want to use within the main script. Using any of the ways above, you can use exports of non-main modules within that plugin.
    Checking whether any plugin is enabled or not would be a task to check via NodeBB modules. Directly attempting to access a plugins exports however should not depend on the internal structure of NodeBB in my opinion.
    If you want to call a plugins exported attributes only iff the plugin is enabled, your way seems to be good though.
    I guess I'd still use

     // would be neat if NodeBB provides a function for this ;-) Accessing NodeBB's non-function attributes directly seems somehow wrong to me
    // proposal: plugins.isEnabled("my-plugin") if nothing alike exists already
    if (plugins.libraries.hasOwnProperty("my-plugin")) {
      require("my-plugin").function();
    }
    

    since it clearly states where the functions source code lies.

    1 Reply Last reply
    2
  • julianJ Offline
    julianJ Offline
    julian GNU/Linux
    wrote on last edited by
    #10

    Ooh, @yariplus @frissdiegurke I hadn't really thought of that, but yes, that'd work really well to enhance/load behaviour only if a peer is loaded...

    ... and only because peer dependencies are such crap on npm.. 😠

    1 Reply Last reply
    0

Copyright © 2023 NodeBB | Contributors
  • Login

  • Don't have an account? Register

  • Login or register to search.
Powered by NodeBB Contributors
  • First post
    Last post
0
  • Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development