Skip to content

Using "require" inside function nodes #41

Closed

Description

@maiorfi

Hi. Is it possible to import/require external modules within a function node?

Thanks!

Activity

  1. hdoukas commented on Oct 15, 2013

    @hdoukas

    Hi,

    Don't think so, you need to create your own node to import/require modules

    On Oct 15, 2013, at 3:32 PM, maiorfi [email protected] wrote:

    Hi. Is it possible to import/require external modules within a function node?

    Thanks!

    —
    Reply to this email directly or view it on GitHub.

  2. knolleary commented on Oct 15, 2013

    @knolleary
    Member

    You cannot use require itself inside a function node, but you can add external modules to the sandbox used to run functions. This is done using the functionGlobalContext setting - have a look at the Global Context section of this page to see an example of adding the os module.

    (Technically, using this same technique I think you could add the require function to the sandbox and thereby use it as you wish... but I've not tried that)

  3. gbrian commented on Aug 29, 2017

    @gbrian

    Just in case, this works for me:

    functionGlobalContext: {
        require:require
    }
    

    Then in my function:

    var require = global.get('require');
    var request = require('request');
    

    Node-RED version: v0.17.5

  4. carlosgonzalez commented on Dec 31, 2017

    @carlosgonzalez

    Hey @gbrian ,
    I've tried your suggestion but it doesn't work,

    This is what I'm getting:
    "TypeError: require is not a function"

    Any suggestion?

  5. dceejay commented on Dec 31, 2017

    @dceejay
    Member

    Read @knolleary post first, including the links, then @gbrian

  6. gbrian commented on Dec 31, 2017

    @gbrian

    @carlosgonzalez
    Testing with latest: [email protected]
    Nodejs: v8.1.0

    Ok, this time is a bit tricky so there should be a reason for it and maybe this way must be discouraged.

    Answering your question, I managed by:

    • Adding this to functionGlobalContext

    module_require: require('./require.js')

    • Creating a module require.js at the same place at the settings file:

    module.exports = {require:require}

    Usage:
    global.get("module_require").require('os')

    I have to encourage you all to follow the official documentation as this way is totally unsupported.

  7. morxGrillmeister commented on May 9, 2018

    @morxGrillmeister

    @carlosgonzalez
    I got the same error as you ("TypeError: require is not a function"), but then succeeded by just restart node-red after changing the setting.js file:
    functionGlobalContext: add this line:
    moment:require('moment'),

    In my function node:
    var moment = global.get('moment');

  8. arobinsongit commented on Oct 26, 2018

    @arobinsongit

    For those still following this you can use this node written by an old colleague

    https://github.com/ntsaini/node-red-contrib-function-npm

    I've used it extensively and it works very well. Best recommendation is to put all of your requires in a single node then save off to the global context and pull them back down when you need them. Otherwise, if you place requires any multiple nodes in your flow it will execute multiple downloads.

  9. akhilendharreddy commented on Dec 5, 2018

    @akhilendharreddy

    @carlosgonzalez
    I got the same error as you ("TypeError: require is not a function"), but then succeeded by just restart node-red after changing the setting.js file:
    functionGlobalContext: add this line:
    moment:require('moment'),

    In my function node:
    var moment = global.get('moment');

    I am facing issue after adding moment:require('moment'), in setting.js file, error: cannot find module 'moment', i have installed moment npm

  10. knolleary commented on Dec 5, 2018

    @knolleary
    Member

    @akhilendharreddy that suggests you didn't install the moment module in the right location.

  11. akhilendharreddy commented on Dec 5, 2018

    @akhilendharreddy

    You cannot use require itself inside a function node, but you can add external modules to the sandbox used to run functions. This is done using the functionGlobalContext setting - have a look at the Global Context section of this page to see an example of adding the os module.

    (Technically, using this same technique I think you could add the require function to the sandbox and thereby use it as you wish... but I've not tried that)

    @akhilendharreddy that suggests you didn't install the moment module in the right location.
    i have installed in my node-red project folder, can you suggest where do you want me to install

  12. hardillb commented on Dec 5, 2018

    @hardillb
    Member

    The page linked to in that quote has the correct location. The Node-RED user dir -> ~/. node-red

  13. etawyy commented on Feb 15, 2019

    @etawyy

    Hi,
    I have the same issue but with a little difference. I need to have not just a require but also with variables (? beginner):
    var ovh = require('ovh')({
    appKey: 'your_app_key',
    appSecret: 'your_app_secret',
    consumerKey: 'your_consumer_key'
    });

    This works perfectly fine on a local instance of node.js

    With bluemix, I have added in bluemixsettings:
    functionGlobalContext: {
    ovh:require('ovh')({
    endpoint: 'ovh-eu',
    appKey: 'key',
    appSecret: 'key',
    consumerKey: 'key'
    })
    },

    and in my function node I call:
    var ovh = global.get('ovh');
    ovh.request('GET', '/sms', function (err, serviceName) (... etc etc )

    when injecting, the node says: "TypeError: Cannot read property 'request' of undefined"
    So ovh doesn't get defined properly, I guess.
    I have tried to put the variables in the function node instead of bluemixsettings, with no more success. "ovh is not a function"

    Can anyone help? Thanks in advance.

  14. knolleary commented on Feb 15, 2019

    @knolleary
    Member

    Hi @etawyy - please use the forum: https://discourse.nodered.org where the wider community will be able to help.

  15. belbirdtech commented on Jan 21, 2020

    @belbirdtech

    @gbrian Hi Gustavo your way of specifying the require in the settings.js file worked for me !! Thanks!

  16. added 7 commits that reference this issue on Jul 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions