Publish Nodes Question

Hello Everyone!

I have some questions while i'm preparing myself to publish my own set of nodes.

Basically i developed the nodes indipendently with its own package.json and here comes the first question:

-Is it possible to publish an npm package with indipendent package.json? Do i have to do a big package.json with all the nodes and dependencies or can i keep the indipendent ones ( i'd like them to be installed altoghether and not singularly). If i can keep the indipendent ones, how can i make them install together in a single npm package?

Basically like this:

โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ examples
    โ”‚   โ”œโ”€โ”€ example-1.json
    โ”‚   โ””โ”€โ”€ example-2.json
โ””โ”€โ”€ node1
    โ”œโ”€โ”€ icons
    โ”‚   โ””โ”€โ”€ my-icon.svg
    โ”œโ”€โ”€ sample.html
    โ”œโ”€โ”€ package.json
    โ””โ”€โ”€ sample.js
โ””โ”€โ”€ node2
    โ”œโ”€โ”€ icons
    โ”‚   โ””โ”€โ”€ my-icon.svg
    โ”œโ”€โ”€ sample.html
    โ”œโ”€โ”€ package.json
    โ””โ”€โ”€ sample.js

And here comes the second question, i've then amde a big package.json with all the nodes and dependencies but when i try to install it from the .node-red folder it doesn't install the dependencies in the package.json and i have to install them by going in the package folder and do npm install.
Is there a way to fix this?

Package.json is composed like this:

{
  "name": "@sss/node-red-sss-dataManipulation",
  "version": "1.0.0",
  "description": "",
  "dependencies": {
		"axios": "^0.27.2",
		"crypto": "^1.0.1",
		"node-rsa": "^1.1.1",
		"rsa-pem-from-mod-exp": "^0.8.4",
		"utf8": "^3.0.0",
		"uuid": "^9.0.0",
		"mysql2": "^2.3.3",
        "pg": "^8.8.0",
		"alasql": "^1.7.3",
		"xlsx": "https://cdn.sheetjs.com/xlsx-0.18.10/xlsx-0.18.10.tgz",
		"sss-server": "file:./sss_server"
	},
  "node-red": {
    "nodes": {
      "addcolumns": "sss_add_columns/addcolumns.js",
	  "create-view": "sss_create_view/create-view.js",
  	  "csv2array": "sss_csv2array/sss-csv2array.js",
	  "db-hub": "sss_db_hub/db-hub.js",
	  "filter": "sss_filter/sss-filter.js",
	  "get-view-values": "sss_get_view_values/get-view-values.js",
	  "joinmatrix": "sss_join_matrix/joinmatrix.js",
	  "merge-messages": "sss_merge_messages/merge-messages.js",
	  "register-notification": "sss_register_notification/register-notification.js",
	  "renamelabels": "sss_rename_labels/renamelabels.js",
	  "sss-server": "sss_server/sss-server.js",
	  "update-view": "sss_update_view/update-view.js",
	  "sss-user": "sss_user/sss-user.js",
	  "xlsx2array": "sss_xlsx2array/sss_xlsx2Array.js",
	  "sss-api": "sss_api/sss-api.js"
    }
  },
  "author": "DocSpace",
  "license": "ISC"
}

Third and final question, if i install them in both folders (the .node-red and the package one) when starting node red a warning is displayed about a plugin node called "sss-api", the warning is this:

[warn] [@sss/node-red-sss-dataManipulation/sss-api] TypeError: Cannot read properties of undefined (reading 'plugins') 

In the code the corrispondent line is : RED.plugins.registerPlugin(PLUGIN_TYPE_ID, {
Everything seems to work fine but it just bothers me.

Bonus question :
The plugin node coudn't be installed if i put it in the package.json file under the section "plugins" so i put it in nodes and it worked, this could be relevan for the previous question.
ex:

"plugins": {
      "sss-api": "sss-api.js"
    }
this does not work giving a similar exception:

TypeError: Cannot read properties of undefined (reading 'forEach')
    at Object.addModule (/usr/src/node-red/node_modules/@node-red/registry/lib/registry.js:195:27)
    at /usr/src/node-red/node_modules/@node-red/registry/lib/loader.js:138:34

Thank you for your time and have a nice day!

To install your sub-dependencies, they would need to be included as dependencies in your master package.json. e.g. "node1": "file:./node1",.

I've never tried that but I think it should work.

Not really needed though since node-red will let you define as many nodes as you like and you'll end up installing all the dependencies anyway so might as well include them in the master package.json.

Now i've made a master package.json, is there a way in a npm package to have m ultiple smaller package.json and to install them together?
Above there's the code of my package.json and ti've listed all my node dependencies there, but still an npm install is needed in the package folder :frowning:

Sorry, I'm being rather slow (just recovering from covid).

You aren't going about this the right way.

If you want to create a package containing multiple node-red nodes and publish that to npm but keep the code for each node in its own folder, that is fine but what you need to do is put all of the dependencies for all nodes in the package. Then the sub-folders describing the nodes are simply referenced in their folders in the node-red section of the package's package.json file.

As in this example from uibuilder where you can see that the uib-list node is defined in its own folder.

Don't you worry, take your time, hope you're recovering well.

I gave a look at what you were saying and this is exactly what i did in the first place. I took all the dependecies of all the nodes in the package.json master file and put all the nodes in their subfolders there as well.
Just like this:

{
  "name": "@sss/node-red-sss-dataManipulation",
  "version": "1.0.0",
  "description": "",
  "dependencies": {
		"axios": "^0.27.2",
		"crypto": "^1.0.1",
		"node-rsa": "^1.1.1",
		"rsa-pem-from-mod-exp": "^0.8.4",
		"utf8": "^3.0.0",
		"uuid": "^9.0.0",
		"mysql2": "^2.3.3",
        "pg": "^8.8.0",
		"alasql": "^1.7.3",
		"xlsx": "https://cdn.sheetjs.com/xlsx-0.18.10/xlsx-0.18.10.tgz",
		"sss-server": "file:./sss_server"
	},
  "node-red": {
    "nodes": {
      "addcolumns": "sss_add_columns/addcolumns.js",
	  "create-view": "sss_create_view/create-view.js",
  	  "csv2array": "sss_csv2array/sss-csv2array.js",
	  "db-hub": "sss_db_hub/db-hub.js",
	  "filter": "sss_filter/sss-filter.js",
	  "get-view-values": "sss_get_view_values/get-view-values.js",
	  "joinmatrix": "sss_join_matrix/joinmatrix.js",
	  "merge-messages": "sss_merge_messages/merge-messages.js",
	  "register-notification": "sss_register_notification/register-notification.js",
	  "renamelabels": "sss_rename_labels/renamelabels.js",
	  "sss-server": "sss_server/sss-server.js",
	  "update-view": "sss_update_view/update-view.js",
	  "sss-user": "sss_user/sss-user.js",
	  "xlsx2array": "sss_xlsx2array/sss_xlsx2Array.js",
	  "sss-api": "sss_api/sss-api.js"
    }
  },
  "author": "DocSpace",
  "license": "ISC"
}

This is now my package.json and i've of course removed the individual packages in each node subfolder.

this is now my folder composition.

When i run from inside the .node-red folder the npm install of the folder containing all the nodes, some nodes won't get installed (becouse of the dependencies) .this is what i get when i start node-red:

In order to get those dependencies installed (i have them listed in the main package.json) i have to go to the folder where the nodes are and run npm install from there. What i wantedwas to avoid this second npm install.

I've watched your package.json here : node-red-contrib-uibuilder/package.json at main ยท TotallyInformation/node-red-contrib-uibuilder ยท GitHub

And i think i did the same thing (except for scripts and folders)

Something not quite right then, you can see that is how I do it and the require module should search back up the tree to find the right node_modules folder. Can you check ~/.node-red/node_modules/ for the mysql2 and alasql folders? Just to make sure they really got installed?

You can see the folder stack that require is attempting to use. To me that looks off. By any chance, did you install node-red using an admin credential instead of user? Or are you running node-red as an admin?

Good morning!
I've installed node-red on windows like this:

npm install -g --unsafe-perm node-red

The subfolder inside.node-red is like this:



I don't know if node red has been installed by an admin, at least not voluntarily.

{
    "name": "node-red-project",
    "version": "0.0.1",
    "lockfileVersion": 2,
    "requires": true,
    "packages": {
        "../Desktop/sss-npm-prova": {
            "version": "1.0.0",
            "license": "ISC",
            "dependencies": {
                "alasql": "^1.7.3",
                "axios": "^0.27.2",
                "crypto": "^1.0.1",
                "mysql2": "^2.3.3",
                "node-rsa": "^1.1.1",
                "pg": "^8.8.0",
                "rsa-pem-from-mod-exp": "^0.8.4",
                "sss-server": "file:./sss_server",
                "utf8": "^3.0.0",
                "uuid": "^9.0.0",
                "xlsx": "https://cdn.sheetjs.com/xlsx-0.18.10/xlsx-0.18.10.tgz"
            }
        },
        "node_modules/node-red-sss-dataManipulation": {
            "resolved": "../Desktop/sss-npm-prova",
            "link": true
        }
    }
}

This is the content of the package-lock.json in image one

And this is the full error:

17 Nov 09:59:12 - [warn] [node-red-sss-dataManipulation/db-hub] Error: Cannot find module 'mysql2'
Require stack:
- C:\Users\aleve\Desktop\sss-npm-prova\sss_db_hub\db-hub.js
- C:\Users\aleve\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\registry\lib\loader.js
- C:\Users\aleve\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\registry\lib\index.js
- C:\Users\aleve\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\runtime\lib\nodes\index.js
- C:\Users\aleve\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\runtime\lib\index.js
- C:\Users\aleve\AppData\Roaming\npm\node_modules\node-red\lib\red.js
- C:\Users\aleve\AppData\Roaming\npm\node_modules\node-red\red.js
17 Nov 09:59:12 - [warn] [node-red-sss-dataManipulation/joinmatrix] Error: Cannot find module 'alasql'

I've reinstalled node-red multiple times in these days, could it be that something got corrupted?

Delete the lock file, it will get re-created, I've had lots of problems with them. It is only the package.json you need to look at.

../Desktop/sss-npm-prova - where has that come from? It seems to be referencing a package from your Desktop folder? "node_modules/node-red-sss-dataManipulation" that's how things should look

1 Like

I've fixed the problem.
The folder with nodes and all was placed outside the .node-red folder, this was causing the problem.

So if i place sss-npm-prova (the folder containing nodes,package.json and all) outside the .node-red folder it won't install the dependencies.
If i place it inside it will work

I have only one last question:

One of the nodes is a plugin node and this is what happens:

when starting node red a warning is displayed about a plugin node called "sss-api", the warning is this:

[warn] [@sss/node-red-sss-dataManipulation/sss-api] TypeError: Cannot read properties of undefined (reading 'plugins') 

In the code the corrispondent line is : RED.plugins.registerPlugin(PLUGIN_TYPE_ID, {
Everything seems to work fine but it just bothers me.

The plugin node coudn't be installed if i put it in the package.json file under the section "plugins" so i put it in nodes and it worked, this could be relevant for the previous question.
ex:

"plugins": {
      "sss-api": "sss-api.js"
    }
this does not work giving a similar exception:

TypeError: Cannot read properties of undefined (reading 'forEach')
    at Object.addModule (/usr/src/node-red/node_modules/@node-red/registry/lib/registry.js:195:27)
    at /usr/src/node-red/node_modules/@node-red/registry/lib/loader.js:138:34

so what i've done is putting it into nodes :slight_smile:

"nodes": {
      "addcolumns": "sss_add_columns/addcolumns.js",
	  "create-view": "sss_create_view/create-view.js",
  	  "csv2array": "sss_csv2array/sss-csv2array.js",
	  "db-hub": "sss_db_hub/db-hub.js",
	  "filter": "sss_filter/sss-filter.js",
	  "get-view-values": "sss_get_view_values/get-view-values.js",
	  "joinmatrix": "sss_join_matrix/joinmatrix.js",
	  "merge-messages": "sss_merge_messages/merge-messages.js",
	  "register-notification": "sss_register_notification/register-notification.js",
	  "renamelabels": "sss_rename_labels/renamelabels.js",
	  "sss-server": "sss_server/sss-server.js",
	  "update-view": "sss_update_view/update-view.js",
	  "sss-user": "sss_user/sss-user.js",
	  "xlsx2array": "sss_xlsx2array/sss_xlsx2Array.js",
	  "sss-api": "sss_api/sss-api.js"
    }

is it good?

Thank you really really much for the time you've dedicated to this

Looks like you might be hitting a known issue: Race condition when loading module with both Plugin and Nodes ยท Issue #3523 ยท node-red/node-red (github.com)

1 Like