# TypeError: Cannot read property 'nodes' of undefined

**URL:** https://discourse.nodered.org/t/typeerror-cannot-read-property-nodes-of-undefined/16084
**Category:** Developing Nodes
**Created:** [30 September 2019 10:41 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-nodes-of-undefined/16084 "2019-09-30T10:41:23Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![gelbo123](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@gelbo123](https://discourse.nodered.org/u/gelbo123)
#### Post date: [30 September 2019 10:41 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-nodes-of-undefined/16084/1 "2019-09-30T10:41:23Z")

</div>

Hello, I have two javascript-files for some custom-nodes...  
Goal is to use the functionality from "hello-world.js" which is a custom-node in the "test.js".

In the first one "hello-world.js" - I'm writing an input from an inject node in a file called hello.txt. This happens in `helloFunction()`.

```auto
var exports = module.exports = {};

exports.oneFunction = function(RED) {

    function helloFunction (config) {
    
        RED.nodes.createNode(this,config);
        const fs = require('fs');
        path = require('path');
        filePath = path.join(__dirname, 'hello.txt');
        var node = this;
        node.on('input', function(msg) {
            var fileContent = msg.payload + '\n';
            fs.appendFile(filePath, fileContent , (err) => { 
                if (err) throw err });
                node.send(msg);    
        });   
    };

    RED.nodes.registerType("hello-world", helloFunction);
}

```

In the second one "test.js" - I'm importing the "hello-world.js".

```auto
 const helloWorld = require("./hello-world");
    
    function testing() {

        helloWorld.oneFunction();
    }

    console.log(testing());

```

When i execute "test.js" I get a `TypeError: Cannot read property 'nodes' of undefined` in the "hello-world.js".

---

<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: [2 October 2019 15:46 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-nodes-of-undefined/16084/2 "2019-10-02T15:46:50Z")

</div>

The problem comes because your `test.js` file is calling `helloWorld.oneFunction();` without any arguments. However, that function expects one argument to be passed in:

```auto
exports.oneFunction = function(RED) {

```

`RED` is the handle to the node-red runtime that is given to nodes when the runtime loads their module.

The error `TypeError: Cannot read property 'nodes' of undefined` means precisely that. The first line of `helloFunction` calls `RED.nodes.createNode(this,config);` but because you haven't passed in `RED`, that will be undefined.

---

<div class="post-metadata">

### Author: ![gelbo123](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@gelbo123](https://discourse.nodered.org/u/gelbo123)
#### Post date: [8 October 2019 07:33 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-nodes-of-undefined/16084/3 "2019-10-08T07:33:49Z")

</div>

I tried it out but now the `TypeError: this.updateWires is not a function` at the file Node.js occures. Any suggestions?

---

<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: [8 October 2019 08:30 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-nodes-of-undefined/16084/4 "2019-10-08T08:30:38Z")

</div>

Without seeing your code, no, I cannot tell you why you are hitting that error.

---

<div class="post-metadata">

### Author: ![gelbo123](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@gelbo123](https://discourse.nodered.org/u/gelbo123)
#### Post date: [8 October 2019 08:36 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-nodes-of-undefined/16084/5 "2019-10-08T08:36:46Z")

</div>

File: test.js

```auto
var http = require('http');
var express = require("express");
var RED = require("node-red");

// Create an Express app
var app = express();

// Add a simple route for static content served from 'public'
app.use("/",express.static("public"));

// Create a server
var server = http.createServer(app);

// Create the settings object - see default settings.js file for other options
var settings = {
    httpAdminRoot:"/red",
    httpNodeRoot: "/api",
    userDir:"/home/nol/.nodered/",
    functionGlobalContext: { } // enables global context
};

// Initialise the runtime with a server and settings
RED.init(server,settings);

// Serve the editor UI from /red
app.use(settings.httpAdminRoot,RED.httpAdmin);

// Serve the http nodes UI from /api
app.use(settings.httpNodeRoot,RED.httpNode);

server.listen(8000);

const helloWorld = require("./hello-world");
    
function testing() {
    
    helloWorld.oneFunction(RED);
}

// Start the runtime
RED.start().then(() => {
    console.log(testing());
});

```

File: hello-world.js

```auto
var exports = module.exports = {};

exports.oneFunction = function(RED) {

    function helloFunction (config) {
    
        RED.nodes.createNode(this,config);
        const fs = require('fs');
        path = require('path');
        filePath = path.join(__dirname, 'hello.txt');
        var node = this;
        node.on('input', function(msg) {
            var fileContent = msg.payload + '\n';
            fs.appendFile(filePath, fileContent , (err) => { 
                if (err) throw err });
                node.send(msg);    
        });   
    };

    RED.nodes.registerType("hello-world", helloFunction);
}

```

---

<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: [8 October 2019 08:41 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-nodes-of-undefined/16084/6 "2019-10-08T08:41:32Z")

</div>

That isn't how Node-RED loads modules. You don't call it manually - you let node-red find your module on the node path and it will load it properly.

---

<div class="post-metadata">

### Author: ![gelbo123](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@gelbo123](https://discourse.nodered.org/u/gelbo123)
#### Post date: [9 October 2019 07:22 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-nodes-of-undefined/16084/7 "2019-10-09T07:22:51Z")

</div>

With calling the module manually you mean the `require("./hello-world");` in the test.js, right?

---

<div class="post-metadata">

### Author: ![gelbo123](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@gelbo123](https://discourse.nodered.org/u/gelbo123)
#### Post date: [16 October 2019 15:36 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-nodes-of-undefined/16084/8 "2019-10-16T15:36:43Z")

</div>

**I quite don't understand what you mean with...**

> ...That isn't how Node-RED loads modules. You don't call it manually - you let node-red find your module on the node path and it will load it properly.

---

<div class="post-metadata">

### Author: ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)
#### Post date: [16 October 2019 15:41 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-nodes-of-undefined/16084/9 "2019-10-16T15:41:33Z")

</div>

Out of curiosity, is there a reason why you don't follow the docs on creating custom nodes, and for shared code between different nodes use a separate javascript module that is imported to both?

---

<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: [16 October 2019 15:42 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-nodes-of-undefined/16084/10 "2019-10-16T15:42:03Z")

</div>

Your code in `test.js` should not be trying to `require` and initialise your node module itself.

The Node-RED runtime is responsible for loading the nodes. The `RED` object that gets passed to the node module function is unique to that module - it is not the `RED` object you get from `require("node-red")`.
