What you will learn in this chapter is a very useful Project feature. The project feature of Node-RED is a kind of version management tool with Git on a Node-RED flow editor. This is actually disabled by default. Enabling this allows you to manage your flows in a new way. I believe many developers are familiar with Git services such as GitHub and GitLab. The project feature of Node-RED uses Git and GitHub for version control, so I think it's very easy to understand.
Here are the topics that we will be covering in this chapter:
By the end of this chapter, you will be able to understand how to use the project feature, how to connect your own Git repository to your Node-RED flow editor, and how to manage flows as projects with version control tool Git.
By the end of this chapter, you will have mastered how to use the project feature and make your applications with it. You can use it in any hosted Git service such as GitHub or GitLab.
To progress in this chapter, you will require the following:
For example, in situations where you want to manage your own flow while sharing it with others, or you want to update the flow created by others, it is difficult to develop when a team uses only the Node-RED flow editor.
The project function of Node-RED is a method/function for managing the files that are relevant with each flow you make. It covers all the files needed to create applications with Node-RED shareable.
These are supported by the Git repository. That is, all files are versioned. This allows developers to collaborate with other users.
On Node-RED version 1.x, the project feature is disabled by default, so it must be enabled in the config file named settings.js.
Important note
When creating a project in the local environment of Node-RED, the flow created so far may be overwritten with a blank sheet. You can download the JSON files of the flow configurations for all the flows created in this document via the internet, but if the flow you created yourself exists in Node-RED in the local environment, it is recommended to export the flow configuration file.
All of the flow definitions and JSON files that we created in this book are available to download here: https://github.com/PacktPublishing/-Practical-Node-RED-Programming.
Now let's try the project function. We will use a standalone version of Node-RED on a local environment such as macOS or Windows. In order to use the project feature, we first need to enable it. Let's enable it by following these steps:
By default, on a Mac, this file is available under the following path:
/Users/<User Name>/.node-red/settings.js.
By default, on Windows, this file is available under the following path:
C:Users<User Name>.node-redsettings.js
$ vi /Users/<User Name>/.node-red/settings.js
Important note
Please replace the command with the one specific to your environment.
module.exports = {
uiPort: process.env.PORT || 1880,
…
editorTheme: {
projects: {
enabled: true
}
},
…
}
$ node-red
We have now successfully enabled the project feature of Node-RED.
To use this feature, you need to have access to Git and ssh-keygen command-line tools. Node-RED checks them at startup and notifies you if any tools are missing.
If the settings are completed without any problems and you have restarted Node-RED, the project feature will be available. Next, let's set up the Git repository to use.
We enabled the project feature in the previous section. Reopen the flow editor and you will be prompted to create your first project using the contents of the flow you created at that time. This will be the welcome screen:
We need to set up a version control client such as Git. As already explained, the project function of Node-RED uses Git as a version control tool. As with regular Git, you can manage file changes on a project-by-project basis and synchronize with remote repositories as required.
Git keeps track of who made the change. It works with your username and email address. The username does not have to be your real name; you can use any name you like.
If your local device already has a Git client configured, Node-RED will look up those settings.
First, perform version control in your local environment. It takes advantage of the features of the Git client installed in your local environment. If you do not have Git installed, please install it in advance.
Now, follow these next steps to create a project on your Node-RED flow editor:
In other words, Node-RED automatically migrates the flow currently configured on the flow editor to a new project as it is. It is OK to keep the default name. Of course, you may choose to rename it here if you so wish.
If you publish your project on a public site such as GitHub, it's a good idea to encrypt your credentials file.
If you choose to encrypt, you must create a key to use for encryption. This key is not included in the project, so if you share the project with someone, you will need to provide the credential file decryption key separately to the user who cloned the project.
Congratulations! You have created your first project.
If you are a regular user of Git or GitHub, you should be able to understand the meaning and role of each item just by looking at the structure of this panel. If there is a change in the file structure or contents under the project, the target file will be displayed in the Local Changes area. When you move the change to the commit stage (that is, when you add it), the display of the target file moves to the Changes to commit area. If you enter a commit message and complete the commit, the version will be incremented by one.
This is exactly the same as what the Git client does.
Following deployment of this flow, you can see the flow.json file in the Local Changes area. This means that a flow consisting of an inject node and a debug node has been added (deployed) on the flow editor, and the flow.json file, which is the configuration file for this entire flow, has been updated. As a result, flow.json has been recognized as a file to be changed in Git management:
You can see that the flow.json file has moved from the Local Changes area to the commit area.
As you learned, after creating your project, it will be possible to use the Node-RED editor the same as usual.
Now, let's add a new user interface to the Node-RED flow editor for project functionality.
The project you are working on will appear at the top of the right-hand pane. Next to the project name, there is also a Show project settings button:
You can also access the Project Settings screen from the Projects | Project Settings option under the main menu:
When the Project Settings panel is shown, you will see that it has three tabs for each setting:
If you want to check and modify the Git settings, you can access the settings panel via the main menu:
Now you know how to version control in your local environment. The next step entails understanding how to connect a remote repository such as a GitHub service.
Now, let's learn how to connect Node-RED to a remote repository such as GitHub. Here, we will use the GitHub service for the remote repository. This is like connecting local Git and remote GitHub via Node-RED. This is nothing special. It is familiar to people who use Git/GitHub on a regular basis, but it's very similar to the situation where GitHub is used as a client tool. It is very easy for you to manage the version with Node-RED.
Create a remote repository of your Node-RED project on GitHub with the help of the following steps:
It's a good idea to use a project name similar to your local repository. We won't go into details of how to use GitHub here, but since it is a service that can be used intuitively, I believe that anyone can use it without any problems:
The remote repository on GitHub is now linked to the Git repository in your local environment. But they are not yet in sync. All you have to do is pull the remote locally and merge it. To do this, select the history panel on the side information menu, and then click the Manage remote branch button on the Commit History panel to connect to your remote repository:
Here, there is a difference between remote and local because we have already created the flow locally and versioned it with local Git. In this case, you need to pull the remote content locally before you can push the local content to the remote.
A message indicating a conflict will be displayed en route, but proceed with the merge as it is. During the merge, you will be asked whether you want to apply the remote changes or the local changes. In that case, apply the changes on the local side to complete the merge.
Following the operation, you will see that your local branch has been merged with your remote branch on the Commit History panel:
Congratulations! Now you have learned how to use the project feature on Node-RED and you can also connect a remote repository from your local repository of Node-RED.
In this chapter, you learned how to enable the project feature of Node-RED and integrate local version control using Git with a remote repository created on GitHub. This will be very useful when you develop a team using Node-RED in the future.
In the next chapter, we will use this project feature to clone the repository of a to-do application locally. By studying this chapter and the next chapter together, you should have a greater in-depth understanding of the project feature.