# Adding npm module and using it

**URL:** https://discourse.nodered.org/t/adding-npm-module-and-using-it/6655
**Category:** General
**Created:** [10 January 2019 10:58 UTC](https://discourse.nodered.org/t/adding-npm-module-and-using-it/6655 "2019-01-10T10:58:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![alanca](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@alanca](https://discourse.nodered.org/u/alanca)
#### Post date: [10 January 2019 10:58 UTC](https://discourse.nodered.org/t/adding-npm-module-and-using-it/6655/1 "2019-01-10T10:58:21Z")

</div>

I want to use the atob function to convert base64 to hex. After googling a little bit I found out that npm modules can be added manually.

I did `npm i atob` in the npm directory, as well as the node-red directory:

`usr/lib/node_modules/npm/node_modules/atob`  
`usr/lib/node_modules/node-red/node_modules/atob`

Next, I added the following to settings.js:

```
functionGlobalContext: {
atob: require('atob)
}

```

In my function node, I added the first line

```
var atob = global.get('atob');
var raw = "oAAABTUAAg==";
var hex = atob(raw);

```

But I get the following error: _"TypeError: atob is not a function"_

What am I doing wrong and/or misinterpreting?

---

<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: [10 January 2019 11:01 UTC](https://discourse.nodered.org/t/adding-npm-module-and-using-it/6655/2 "2019-01-10T11:01:04Z")

</div>

You need to run the `npm install atob` command in the same directory as your settings file.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [10 January 2019 11:27 UTC](https://discourse.nodered.org/t/adding-npm-module-and-using-it/6655/3 "2019-01-10T11:27:47Z")

</div>

though we do have a base64 node to do conversions already - node-red-node-base64

---

<div class="post-metadata">

### Author: ![alanca](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@alanca](https://discourse.nodered.org/u/alanca)
#### Post date: [10 January 2019 12:34 UTC](https://discourse.nodered.org/t/adding-npm-module-and-using-it/6655/4 "2019-01-10T12:34:44Z")

</div>

Thanks, that did the trick! (node-red-node-base64)

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [11 January 2019 15:58 UTC](https://discourse.nodered.org/t/adding-npm-module-and-using-it/6655/5 "2019-01-11T15:58:49Z")

</div>

Another way to do this, say inside some existing function code, is with the built-in JS Buffer functions:

```auto
var encoded = Buffer.from(msg.payload).toString("base64");
var decoded = Buffer.from(encoded, "base64").toString();

```

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [11 January 2019 16:39 UTC](https://discourse.nodered.org/t/adding-npm-module-and-using-it/6655/6 "2019-01-11T16:39:42Z")

</div>

As a last resort if you want to install atob from npm then it should work (it did for me) if you correct the missing quote in the require line in `settings.js`:

```auto
functionGlobalContext: {
atob: require('atob')
}

```
