Node-RED lints when developing nodes

Is there a way to get proper linting when developing nodes? I have no type information about the 'RED' variable that is used with the Node-RED functions.

Is there some dev dependency I can add? Or something else I need to configure.

I know my way around javascript the language, but the whole ecosystem is still very unclear to me.

I use vscode as my editor if that helps.

Thanks in advance

There is @types/node-red which you can install as devDependency.

After installation vscode will be able to search for types.

1 Like

Not sure that gets updated much does it?

I think that the current info is in the node-red package itself. so you might need to install it as a dev dependency for each node you are developing.

Personally, I don't like to do that so UIBUILDER has its own typedef defined. That includes some basic node-red stull but also all of the extended definitions of my own nodes including runtime and editor.

image

Not perfect but useful to me at least.

Examples of use in a node runtime:
image
image

I haven't checked for v4 but it was up to date last time I looked at.

I know what usually changes is the RED.util part.

I think Nick would like NR to be (more) typed but it takes time (if I remember correctly the discussion).

1 Like

installed it,
RED still has type: any, and no method signatures.
So that doesn't seem to work or I'm missing something, I did npm install after adding it to my package.json under "devDependencies".

It gives some audit warnings though

I tried adding "node-red" as a devDependency but this didn't seem to help unfortunately

You can use as follow:

/**
 * @type {import("node-red").NodeAPI} RED
 */
var RED;

/**
 * @param {import("node-red").NodeAPI} red
 */
function random(red) {}

It use the @types/node-red package

1 Like

Okay I found an example and they just use "RED.nodes as any" rip

Do you use JavaScript or TypeScript? as any is a TS thing.

Yes! that seems to work, thanks a lot!

Currently my nodes are still vanilla javascript, but I am testing the waters with typescript

Anyone is welcome to use the definitions in uibuilder. :grin:

Perhaps it is. Somewhat confusing that it's version doesn't match the Node-RED version:

It is currently v1.3.5

1 Like

You can always do what I do and add a typedefs.js file in the root of your package. I just use JSDoc definitions in there, VScode is quite happy with that.

Feel free to use the file in the uibuilder package as a starting point.

I have to say - just my personal opinion of course - that the reason I switched to using Node.js from PHP over a decade a go was that I didn't want to spend time learning multiple languages and I already had to know JavaScript for the browser.

So for me, using Typescript would be a serious backwards step. JavaScript with VSCode and decent use of JSDoc definitions seems to do everything I would want from Typescript without having to write double the number of files with significantly more complex grammar (JS is bad enough).

its already too late for not learning multiple languages haha, I have to use matlab, c, zig, rust, python,javascript, bash on a day to day base. Okay zig and rust are not mandatory, I wormed those in but yeah, I really like statically typed languages, I've tried some typescript and so far liking it a lot more than javascript.

Fair enough and if you use C/C++ I suspect that makes TypeScript rather more acceptable.

Nobody doubts that JavaScript is a fairly horrid language. But it is still infinitely nicer to use (in my highly biased opinion :grinning: ) than C/C++.

To be fair, some of JS's horrors lie not in the language but in the HTML API's some of which are appalling.

But hey, as I say, I can't avoid JS and after so long, I can't be bothered to learn a new language - I'm not a professional developer after all (used to be, a very, very long time ago now).

If I were going to learn (re-learn actually) another server-side language, it would be to learn more Python again. I know just enough C/C++ to be able to do stuff on Microcontrollers if I have to. I use BASH and PowerShell where I need to, thankfully not too often. I do miss FORTH and APL though :rofl:

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