You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
Reacted by Gustavo Brian, Stephen J. Carnam and Alex Gorbatchev
var require = global.get('require');
var request = require('request');
Node-RED version: v0.17.5
Reacted by Marcio Granzotto Rodrigues, Thiago Bustamante, emile, tilleul, Jo Torsmyr, Allahbaksh Mohammedali Asadullah, Miloš Jovac, Nurgak, Nick Phillips, davids1802 and 29 more
@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');
Reacted by Carlos Gonzalez, David Roth and Marcel Fitzner
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.
@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
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
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"
Activity
hdoukas commentedon Oct 15, 2013
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:
knolleary commentedon Oct 15, 2013
You cannot use
requireitself inside a function node, but you can add external modules to the sandbox used to run functions. This is done using thefunctionGlobalContextsetting - have a look at the Global Context section of this page to see an example of adding theosmodule.(Technically, using this same technique I think you could add the
requirefunction to the sandbox and thereby use it as you wish... but I've not tried that)gbrian commentedon Aug 29, 2017
Just in case, this works for me:
Then in my function:
Node-RED version: v0.17.5
carlosgonzalez commentedon Dec 31, 2017
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?
dceejay commentedon Dec 31, 2017
Read @knolleary post first, including the links, then @gbrian
gbrian commentedon Dec 31, 2017
@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:
functionGlobalContextmodule_require: require('./require.js')require.jsat 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.
morxGrillmeister commentedon May 9, 2018
@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');
arobinsongit commentedon Oct 26, 2018
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.
akhilendharreddy commentedon Dec 5, 2018
I am facing issue after adding moment:require('moment'), in setting.js file, error: cannot find module 'moment', i have installed moment npm
knolleary commentedon Dec 5, 2018
@akhilendharreddy that suggests you didn't install the moment module in the right location.
akhilendharreddy commentedon Dec 5, 2018
hardillb commentedon Dec 5, 2018
The page linked to in that quote has the correct location. The Node-RED user dir ->
~/. node-redetawyy commentedon Feb 15, 2019
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.
knolleary commentedon Feb 15, 2019
Hi @etawyy - please use the forum: https://discourse.nodered.org where the wider community will be able to help.
etawyy commentedon Feb 15, 2019
Done here: https://discourse.nodered.org/t/using-require-in-bluemix-function-nodes/8005
Sorry and thanks.
belbirdtech commentedon Jan 21, 2020
@gbrian Hi Gustavo your way of specifying the require in the settings.js file worked for me !! Thanks!
node-red#41 node.js v22対応
node-red#41 GHA修正
node-red#41 GHAをもとに戻す
node-red#41 差分の解消
node-red#41 依存関係の修正
node-red#41 gruntのverifyPackageDependenciesでエラーが出るためoverridesを削除
node-red#41 リファクタリング