- 1.) What is this?
- 2.) The difference between this package and node-red-contrib-typescript-template (READ THIS!)
- 3.) Installation
- 4.) Usage
- 5.) Credits and References
node-red-typescript-essentials is a collection of TypeScript classes and functions to facilitate the creation of Node-RED extension nodes with TypeScript.
Its main component is the AbstractNode
class. AbstractNode
is a combination of TS type definition, wrapper and extension of Node-RED's JavaScript node module structure. You can find an introduction about how to define a Node-RED node using Node RED's original JavaScript syntax here: https://node-red.org/docs/creating-nodes/first-node .
AbstractNode
provides a TypeScript/EcmaScript 6 class syntax based "framework" to write Node-RED nodes in a modern and type-checked way. In addition to this role, AbstractNode
also provides a couple of additional features which are not available in the original Node-RED API, like, for example, the getPrevNodes()
and getNextNodes()
methods. As their names say, these methods retrieve the previous and next nodes (by defined node connections) of a node. These additions simplify and accelerate the development of more advanced node behaviour.
We provide another Node-RED and TypeScript-related npm package named node-red-contrib-typescript-template on our GitHub account:
https://github.com/geonet-mrn/node-red-contrib-typescript-template
The two packages are closely related, but they do different things. Please read this section carefully, it is important to understand in order to make the best use of the two projects and get started quickly and without unnecessary confusion.
node-red-typescript-essentials, the package for which you are currently reading the README file, is a library that provides "building blocks" to create Node-RED extensions with TypeScript quickly and easily. However, this library is NOT a project/package template for a Node-RED extension. It is meant to be included as a dependency in a Node-RED extension package project.
This is where node-red-typescript-template, our other package, comes into play. node-red-typescript-template is an example/template for Node-RED extensions using TypeScript, and it uses node-red-typescript-essentials as an external depencency.
The idea behind this is to be able to include node-red-typescript-essentials as an external dependency in many different Node-RED extension projects. By keeping the library as an external dependency, it can be developed independently of actual Node-RED extension packages, and the projects that use it can automatically benefit from additions and improvements to it.
This package is not yet published on npm as of 2020-10-28.
However, with the following command, you can install this package directly from GitHub:
npm install geonet-mrn/node-red-typescript-essentials
This will install the latest version of the master branch.
The development of a Node-RED extension with node-red-typescript-essentials consists of the followings steps:
-
Set up a new npm package project with
npm init
-
Add node-red-typescript-essentials as a dependency with
npm install geonet-mrn/node-red-typescript-essentials
-
Write the code for your new node(s). For a quick start, you can use the command line tool 'node-red-typescript-codegen', which we provide as a separate repository ( https://github.com/geonet-mrn/node-red-typescript-codegen ).
-
Register your new nodes in the
package.json
file of your package. You do this by adding the following structure to yourpackage.json
:
...
"node-red" : {
"nodes": {
"my-node-name": "path/to/my/node/file.js"
}
}
...
- Compile your project to JavaScript. Your
tsconfig.json
should look like this:
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"outDir": "./dist",
"rootDir": "./src",
"target": "es5",
"strict" : true,
"experimentalDecorators": true
},
"exclude": [
"dist",
"node_modules"
]
}
This TypeScript library npm package was created following the instructions described here:
https://www.tsmean.com/articles/how-to-write-a-typescript-library/