npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-red-contrib-file-function

v1.1.2

Published

Read function javascript from file. This way you can use your favorite editor/IDE to develop Node-RED functions

Downloads

271

Readme

node-red-contrib-file-function

This Node-RED node is just like the core node "function", only that this node loads the script to be executed from an actual file on your drive.

This may help you developing for Node RED. Instead of having to write your Javascript code in that small textfield in your browser you can use your favorite editor/IDE.

screenshot of settings

Status

What? | Status | What? | Status ------- | ------ | ------- | ------ Code Climate GPA | Code Climate | Licence | Licence Codacy | Codacy Badge | Tag | Tag Issues | Issues | GitHub Forks | Forks GitHub Version | GitHub version | GitHub Followers | Followers NPM Version | npm version | Dependencies | Dependencies

Filename

The file path will be relative from the path set in settings.userDir, or if not set from the Node-RED install directory.

Either set the filename in the configuration dialog of the node, or override it by the msg.filename property of the incoming message.

Cache

By checking the "Reread file from disk every time node is invoked?" checkbox the file will be read every time the node is called, so there's no need to redeploy or restart Node-RED. If the checkbox is unchecked, the file will be read on deploy/start.

If the checkbox is set to only read the file once (when flow is deployed/Node-RED is started) but another filename is sent in msg.filename, it will read the file from disk and cache it anyways. Only the last called file will be cached. If you're alternating between two different files you're better of creating two different nodes if you're looking for perfomance.

Unless you're working with functions called very often or with very large functions you can probably just leave it to reload the file every time it's invoked.

Writing functions

Writing functions in this node works works just like functions in the the original function node (except that you write it in an actual file and no in an input field):

The message is passed in as a JavaScript object called msg.

By convention it will have a msg.payload property containing the body of the message.

The function should return the messages it wants to pass on to the next nodes in the flow. It can return:

  • a single message object - passed to nodes connected to the first output
  • an array of message objects - passed to nodes connected to the corresponding outputs

If any element of the array is itself an array of messages, multiple messages are sent to the corresponding output.

If null is returned, either by itself or as an element of the array, no message is passed on.

See the online documentation for more help.

Sample

Create a file called sample-file-function.js in your Node-RED root folder. Add the following content to that file:

var reversedPayload = msg.payload.split("").reverse().join("");

return {
    payload: 'This is the input payload, but reversed: ' + reversedPayload
};

screenshot of sample flow

Import this flow (or add it manually by creating a simple [inject] > [file function] > [debug] flow yourself. Add the value sample-file-function.js to the filename field in the file function node):

[{"id":"62efe026.9d102","type":"inject","name":"Inject","topic":"this is topic from the function","payload":"this data is feeded to the function","payloadType":"string","repeat":"","crontab":"","once":false,"x":303,"y":119,"z":"dd1ad5c3.22e528","wires":[["fd11ceda.02ee3"]]},{"id":"fd11ceda.02ee3","type":"file function","name":"","filename":"sample-file-function.js","outputs":"1","x":508,"y":119,"z":"dd1ad5c3.22e528","wires":[["7e85f5db.817a0c"]]},{"id":"7e85f5db.817a0c","type":"debug","name":"","active":true,"console":"false","complete":"true","x":723,"y":118,"z":"dd1ad5c3.22e528","wires":[]}]

Deploy the changes and click the inject node - check the output in the debug sidebar!