Using node.js module in node-red

I have a node.js module which I can read a smartcard UID - I'm running on Windows so none of the NFC/PCSC modules seem to work. I have seen that I can add the module in settings.js to refer to it in node-red but I'm not sure how to get the data from the module. Do I create a msg.payload or a variable which node-red can read? At the moment it just prints the data to console so I haven't tried to incorporate it yet. Any pointers would be helpful.
I've been using MQTT to get the details in but want to move away from that. Once I've read the UID I check for it in an Excel spreadsheet and display the user details in a dashboard.

Are you running v1.3.x?

This feature is much easier to integrate modules : functionGlobalContext

Yuk. At least store details in a database or behind a rest API. Also, have you considered GDPR (or your regions equivalent)?

As I said at the start
Additional node modules cannot be loaded directly within a Function node. They must be loaded in your settings.js file and added to the functionGlobalContext property.
but it doesn't explain how to pass data from the js. This isn't a 'standard' npm module but one that I have written to try to get a simple system to read the smart cards. I've various programmes (in Python and others) which will read the cards but the node-red dashboard is the easiest to display the results.
I am well aware of GDPR. The laptop is fingerprint protected, the spreadsheet is only being used for testing purposes (and then only to check that their membership is still valid and that they have paid for the event - their name doesn't appear in the spreadsheet just the card ID)

sorry, I meant functionExternalModules (which is about 2 inches below the functionGlobalContext on the same page).

Like any module you require you must export the functionality from the js, then you simply call the imported function. Are you familiar with node-js, import/export and require?

How big is the code? You could simply paste a copy into a function node and use it.
As for how to use functionExternalModules with your own module, you could probably place a copy of your project folder into C:\Users\xxxx\.node-red\externalModules\node_modules or if your js is in a properly formatted project you can npm install c:/path/to/your/project. Are you familiar with that concept or do you want more info?

I've been using node-red for about 3 years but only with 'standard' nodes. I've written a few function nodes to handle MQTT,etc. The code is only 50 lines but the first line is
var pcsc = require('pcsclite');
which throws an error. You mention of 'require' prompted me to find node-red-contrib-functionx which has partly solved the problem in that it installs pcslite. The function node reads the card (and the log messages appear in console) but it doesn't automatically transmit the payload so I have to manually trigger the read but I'm sure that I can figure that out.

Thanks for your help.

So as pcsclite is in npm, there is no need to install node-red-contrib-functionx you simply use the function node...


then use it in your code...


Great that helps

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.