# Getting "TypeError: global.get is not a function" when trying to use an NPM package

**URL:** https://discourse.nodered.org/t/getting-typeerror-global-get-is-not-a-function-when-trying-to-use-an-npm-package/43834
**Category:** Developing Nodes
**Created:** [7 April 2021 22:11 UTC](https://discourse.nodered.org/t/getting-typeerror-global-get-is-not-a-function-when-trying-to-use-an-npm-package/43834 "2021-04-07T22:11:18Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![vegabook](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vegabook/32/36284_2.png) [@vegabook](https://discourse.nodered.org/u/vegabook)
#### Post date: [7 April 2021 22:11 UTC](https://discourse.nodered.org/t/getting-typeerror-global-get-is-not-a-function-when-trying-to-use-an-npm-package/43834/1 "2021-04-07T22:11:18Z")

</div>

I'm trying to use the [phoenix-channels](https://www.npmjs.com/package/phoenix-channels) package on npm inside a node that I'm creating. I'm following [the instructions](https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules). I've got it working fine in nodejs on its own.

```auto
const { Socket } = require('phoenix-channels')

let socket = new Socket("https://192.168.1.113:4445/socket")

socket.connect()

// Now that you are connected, you can join channels with a topic:
let channel = socket.channel("room:lobby", {})

channel.on("new_msg", payload => {
  console.log(`${payload.body}`);
});

channel.join()
  .receive("ok", resp => { console.log("Joined successfully", resp) })
  .receive("error", resp => { console.log("Unable to join", resp) })

```

I've installed phoenix channels under the `~/.node-red` and I can see it:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/5/c5f875c2d05cf1a7d822942ff9b99b3e4ac91f75.jpeg)

I've added it to my settings.js file:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/4/44b0a8f38cebfbc1554fb365bd4a58a2176126e5.jpeg)

Now the instructions say I can use it by referencing the "phxModule" which is how I named it in the above settings.js file. My node does appear in the editor, but I get this error:

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

Now (and I'm not a javascript expert), what's wrong with the node code that I'm using? I thought I was supposed to, as per the instructions linked above, reference it using `global.get`?

```auto
🦎tbrowne@nano:~/code/node-red-nodes/phoenix$ cat phoenix.js
module.exports = function(RED) {
  function PhoenixNode(config) {
    RED.nodes.createNode(this,config);
    var node = this;
    
    node.on('input', function(msg) {
        msg.payload = msg.payload.toLowerCase();
        node.send(msg);
    });

    const { Socket } = global.get('phxModule');
    let socket = new Socket("https://192.168.1.113:4445/socket");
    socket.connect();
    let channel = socket.channel("room:lobby", {});
    console.log("hello from phoenix node");
    channel.on("new_msg", payload => {
      node.send({"payload": payload});
    });
    channel.join()
      .receive("ok", resp => { node.send({"payload": resp}) })
      .receive("error", resp => { node.send({"payload": "Unable to join"}) })
  }

  RED.nodes.registerType("phoenix",PhoenixNode);
}

```

Why am I getting this `TypeError: global.get is not a function` issue?

---

<div class="post-metadata">

### Author: ![chrisn-au](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisn-au/32/25504_2.png) [@chrisn-au](https://discourse.nodered.org/u/chrisn-au)
#### Post date: [7 April 2021 23:10 UTC](https://discourse.nodered.org/t/getting-typeerror-global-get-is-not-a-function-when-trying-to-use-an-npm-package/43834/2 "2021-04-07T23:10:11Z")

</div>

Hi,

I am guessing as I have never created a module however I think to access the contexts you would need to do

```auto
const globalContext = node.context().global
const { Socket } = globalContext.get('phxModule');

```

I am not sure but give it a go

Chris

---

<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: [7 April 2021 23:24 UTC](https://discourse.nodered.org/t/getting-typeerror-global-get-is-not-a-function-when-trying-to-use-an-npm-package/43834/3 "2021-04-07T23:24:46Z")

</div>

The functionGlobalContext object is used to expose modules to the Function node.

If you are creating a custom node, you can require it directly in your code - do not use global context.

---

<div class="post-metadata">

### Author: ![chrisn-au](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisn-au/32/25504_2.png) [@chrisn-au](https://discourse.nodered.org/u/chrisn-au)
#### Post date: [7 April 2021 23:33 UTC](https://discourse.nodered.org/t/getting-typeerror-global-get-is-not-a-function-when-trying-to-use-an-npm-package/43834/4 "2021-04-07T23:33:40Z")

</div>

Nick, thanks - Is this documented ? - I could not see it in the creating nodes section of the documentation. Happy to create a pull request to add if appropriate

Chris

---

<div class="post-metadata">

### Author: ![vegabook](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vegabook/32/36284_2.png) [@vegabook](https://discourse.nodered.org/u/vegabook)
#### Post date: [7 April 2021 23:39 UTC](https://discourse.nodered.org/t/getting-typeerror-global-get-is-not-a-function-when-trying-to-use-an-npm-package/43834/5 "2021-04-07T23:39:00Z")

</div>

agreed. This wasn't clear in my (admittedly new user) opinion.

---

<div class="post-metadata">

### Author: ![vegabook](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vegabook/32/36284_2.png) [@vegabook](https://discourse.nodered.org/u/vegabook)
#### Post date: [7 April 2021 23:41 UTC](https://discourse.nodered.org/t/getting-typeerror-global-get-is-not-a-function-when-trying-to-use-an-npm-package/43834/6 "2021-04-07T23:41:41Z")

</div>

works like a dream! Now [Elixir Phoenix channels](https://hexdocs.pm/phoenix/channels.html) can talk to Node Red. Might doll up this node and publish it once I've added all the bells and whistles!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [21 April 2021 23:42 UTC](https://discourse.nodered.org/t/getting-typeerror-global-get-is-not-a-function-when-trying-to-use-an-npm-package/43834/7 "2021-04-21T23:42:17Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
