# Library and node in one package

**URL:** <https://discourse.nodered.org/t/library-and-node-in-one-package/61548>\
**Category:** Developing Nodes\
**Created:** [20 April 2022 13:17 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548 "2022-04-20T13:17:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![NetHans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nethans/32/2843_2.png) [@NetHans](https://discourse.nodered.org/u/NetHans)\
**Post date:** [20 April 2022 13:17 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/1 "2022-04-20T13:17:43Z")

</div>

Hello,  
I am currently trying to create a Node. To make it easier to work with the resulting data, I wanted to add a NodeJs library to the node. This library should be able to be included in the function node via setup, for example.  
But the library is not found by the Node-RED. I always get this message:

```auto
Installation of module TestLib.js failed: module not found
Failed to load external modules required by this flow: 
- TestLib.js [404]

```

In the package.json of the node I specified the library under the entry "main":

```auto
{
   ...
   "main": "TestLib.js",
   }, "node-red": {
      ...
   },
   ...
}

```

The library currently contains only one test function with the following content:

**TestLib.js:**

```auto
function add(){
    console.log("Hello World");
}
module.exports = add()

```

Is it at all possible to place a library and a node in the same package? If it is possible, what do I have to do to use the library in the function node?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [20 April 2022 20:11 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/2 "2022-04-20T20:11:17Z")

</div>

> [@NetHans](#):
>
> trying to create a Node

> [@NetHans](#):
>
> in the function node

Are you trying to create a contributed node or are you trying to use a function node? I suspect that you are trying to use a function node? If so, you question should be under "General" not "Creating Nodes".

> [@NetHans](#):
>
> the library is not found by the Node-RED

Because you are trying to reference a JavaScript file. To use the function node's module loader, you need to specify a package published on npm.

To use a single file, you should `require` it to the globals section of your settings.js and then you can `global.get` it in your function node.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [20 April 2022 20:33 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/3 "2022-04-20T20:33:59Z")

</div>

> [@TotallyInformation](#):
>
> Are you trying to create a contributed node or are you trying to use a function node

I think the question is fairly clear. They are creating a node _and_ want that module to _also_ provide a library that could be accessed in the function node.

> [@NetHans](#):
>
> `Installation of module TestLib.js`

@NetHans what are you putting in the setup tab of the Function node? It should be the name of your module - this error suggests you have entered TestLib.js - the name of the individual file, not the module

---

<div class="post-metadata">

**Author:** ![NetHans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nethans/32/2843_2.png) [@NetHans](https://discourse.nodered.org/u/NetHans)\
**Post date:** [21 April 2022 04:24 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/4 "2022-04-21T04:24:20Z")

</div>

> [@TotallyInformation](#):
>
> Are you trying to create a contributed node or are you trying to use a function node? I suspect that you are trying to use a function node? If so, you question should be under "General" not "Creating Nodes".

I would like to create both. A node and a library. I want to be able to use the library in the function nodes to make it easier to work with the msg data from my node.

> [@knolleary](#):
>
> @NetHans what are you putting in the setup tab of the Function node? It should be the name of your module - this error suggests you have entered TestLib.js - the name of the individual file, not the module

I have already tried both ways to register the library in the function node. First I tried to register the library by the name given in the package.json. When that didn't work, I tried it with the filename of the library. From the last try is the excerpt from the log.

My node is located in the directory for nodes, which is defined in `settings.js` under the property `nodesDir`

```auto
{
   "name": "node-red-contrib-test-node",
   ...
   "main": "TestLib.js",
   }, "node-red": {
      ...
   },
   ...
}

```

**Try 1:**  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/7834e4d0896b838c9bf3902ea68a4f363f966222.png)

**Try 2:**  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/53b0a2c922e522db2b9c92c2ae936f41713ecb40.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 April 2022 06:07 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/5 "2022-04-21T06:07:37Z")

</div>

What you have done with Try 1 should work. If you didn't get any error messages, in what way did it not work? What code do you have in the function node trying to use the library?

---

<div class="post-metadata">

**Author:** ![NetHans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nethans/32/2843_2.png) [@NetHans](https://discourse.nodered.org/u/NetHans)\
**Post date:** [21 April 2022 06:59 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/6 "2022-04-21T06:59:56Z")

</div>

I still get the error message 404.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/2/f2ab3a21491714ed2c237553a17dad466480a28b.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/d/1d0c8cc00d29590423d3a38bbf153b45b9b119b1.png)

In the function node I have stored the following source code:

```auto
nodeRedContribTestNode.add();
return msg;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 April 2022 07:14 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/7 "2022-04-21T07:14:11Z")

</div>

Is `node-red-contrib-test-node` already installed in your node-red?

OR

Is `node-red-contrib-test-node` published to npm?

---

<div class="post-metadata">

**Author:** ![NetHans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nethans/32/2843_2.png) [@NetHans](https://discourse.nodered.org/u/NetHans)\
**Post date:** [21 April 2022 07:19 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/8 "2022-04-21T07:19:25Z")

</div>

no the whole package has not been installed via `npm install`. Also the package is not public.

I only created a folder in the `nodesDir` and put all my files there. The node is also properly recognized by Node-RED and can be used. Only the library is not accessible.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 April 2022 07:28 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/9 "2022-04-21T07:28:40Z")

</div>

I don't think the function node setup imports check that folder?

Would you be willing to try an NPM install to your source ?

E.g. remove your copy from nodesDir & do `npm i /path/to/your/package`

See if that works?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 April 2022 08:42 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/10 "2022-04-21T08:42:50Z")

</div>

Yes, I think that is currently the only way. I tried the other day to install something from GitHub and it didn't like it. But don't think that it strips it from the function node's setup so you probably need to let the auto-install fail and then install the package manually.

---

<div class="post-metadata">

**Author:** ![NetHans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nethans/32/2843_2.png) [@NetHans](https://discourse.nodered.org/u/NetHans)\
**Post date:** [21 April 2022 08:52 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/11 "2022-04-21T08:52:22Z")

</div>

> [@Steve-Mcl](#):
>
> I don't think the function node setup imports check that folder?
> 
> Would you be willing to try an NPM install to your source ?
> 
> E.g. remove your copy from nodesDir & do `npm i /path/to/your/package`
> 
> See if that works?

That didn't work either. The node can be used again. But I have no access to the library. The error message is the same.

My package can now be found under the Node-RED main directory under `node_modules`.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 April 2022 08:54 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/12 "2022-04-21T08:54:31Z")

</div>

Can you share more info about the project (the package.json, the structure etc)  
better still, upload it to github & I'll take a look.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 April 2022 08:55 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/13 "2022-04-21T08:55:07Z")

</div>

To use a module in the function node, you have to have an default exports in the module. You also need to specify the `main` entry point (in the package.json file) as the module file that you want loaded. Remember that this may well be different to your node's js file, in fact it should be.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 April 2022 08:57 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/14 "2022-04-21T08:57:28Z")

</div>

> [@TotallyInformation](#):
>
> o use a module in the function node, you have to have an default exports in the module. You also need to specify the `main` entry point (in the package.json file) as the module file that you want loaded.

All of which is shown in the original post.

---

<div class="post-metadata">

**Author:** ![NetHans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nethans/32/2843_2.png) [@NetHans](https://discourse.nodered.org/u/NetHans)\
**Post date:** [21 April 2022 09:02 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/15 "2022-04-21T09:02:07Z")

</div>

> [@Steve-Mcl](#):
>
> Can you share more info about the project (the package.json, the structure etc)  
> better still, upload it to github & I'll take a look.

I will reduce the package to a minimum to keep the overview. The functionality of the node is irrelevant, because this part of the package works without problems.

I will make the example available here.

> [@TotallyInformation](#):
>
> To use a module in the function node, you have to have an default exports in the module. You also need to specify the `main` entry point (in the package.json file) as the module file that you want loaded. Remember that this may well be different to your node's js file, in fact it should be.

Thanks for the tips, I will take them into consideration.

---

<div class="post-metadata">

**Author:** ![NetHans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nethans/32/2843_2.png) [@NetHans](https://discourse.nodered.org/u/NetHans)\
**Post date:** [21 April 2022 09:46 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/16 "2022-04-21T09:46:27Z")

</div>

I have published the test node on GitHub.

> **[GitHub - NetHans/node-red-contrib-test-node: Test Node with library](https://github.com/NetHans/node-red-contrib-test-node)**
>
> Test Node with library. Contribute to NetHans/node-red-contrib-test-node development by creating an account on GitHub.

I used the following example as the test flow.

```auto
[
    {
        "id": "79e80abe4ac375cb",
        "type": "node-red-contrib-test-node",
        "z": "e8e2da1a3d5d8ace",
        "name": "",
        "x": 270,
        "y": 240,
        "wires": [
            [
                "f079f7b2046c3bc7"
            ]
        ]
    },
    {
        "id": "46e2301134010486",
        "type": "inject",
        "z": "e8e2da1a3d5d8ace",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 240,
        "wires": [
            [
                "79e80abe4ac375cb"
            ]
        ]
    },
    {
        "id": "80ad50bf4395e5b8",
        "type": "debug",
        "z": "e8e2da1a3d5d8ace",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 570,
        "y": 240,
        "wires": []
    },
    {
        "id": "f079f7b2046c3bc7",
        "type": "function",
        "z": "e8e2da1a3d5d8ace",
        "name": "",
        "func": "nodeRedContribTestNode.add()\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [
            {
                "var": "nodeRedContribTestNode",
                "module": "node-red-contrib-test-node"
            }
        ],
        "x": 420,
        "y": 240,
        "wires": [
            [
                "80ad50bf4395e5b8"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 April 2022 12:05 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/17 "2022-04-21T12:05:45Z")

</div>

OK, so that tries to deliver both a node-red node AND a function? I suspect that may be the issue.

If you want a node.js function that you can share with the function node, write that as a pure node.js module, not a node-red custom node.

If you need to also use the same function inside your node, use a standard `require` to reference it.

Don't try to do both in a single file.

---

<div class="post-metadata">

**Author:** ![NetHans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nethans/32/2843_2.png) [@NetHans](https://discourse.nodered.org/u/NetHans)\
**Post date:** [21 April 2022 12:18 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/18 "2022-04-21T12:18:39Z")

</div>

The library and the node belong together. A node can be installed very easily via the Node-RED GUI. A non-public library is not so easy to install. In the production system the automatic installation of libraries is disabled (functionExternalModules: false).

My goal is to ship the combination of node and library and provide both with one installation. It should be as simple as possible. Ideally without using the command line on the server.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 April 2022 12:24 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/19 "2022-04-21T12:24:49Z")

</div>

Sorry, I obviously wasn't clear enough.

You can keep both files in the same PACKAGE which is what npm installs to your userDir folder whether for a custom node installation _or_ for the function node. Node-RED loads the right file using the node-red section of package.json, but the default for the function node should be taken from the `main` property. So you can fulfill both requirements from a single package.

Though you should bear in mind that I've not actually _tested_ this 🙂 . However, that I think should work.

The name you put into the function node must be the full npm name though. So you will likely need to change the "Import As" field to what you expect to use in the function code, otherwise you will end up with a very long name in your code.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 April 2022 12:47 UTC](https://discourse.nodered.org/t/library-and-node-in-one-package/61548/20 "2022-04-21T12:47:13Z")

</div>

@TotallyInformation everything you describe there is exactly what they are doing... but it isn't working.

This needs some proper investigation to figure out why it isn't working.

[Next page](https://discourse.nodered.org/t/library-and-node-in-one-package/61548.md?page=2)
