# Calling a server API in custom node's js

**URL:** <https://discourse.nodered.org/t/calling-a-server-api-in-custom-nodes-js/31204>\
**Category:** General\
**Created:** [6 August 2020 06:45 UTC](https://discourse.nodered.org/t/calling-a-server-api-in-custom-nodes-js/31204 "2020-08-06T06:45:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pavithra\_nayak](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@Pavithra\_nayak](https://discourse.nodered.org/u/Pavithra_nayak)\
**Post date:** [6 August 2020 06:45 UTC](https://discourse.nodered.org/t/calling-a-server-api-in-custom-nodes-js/31204/1 "2020-08-06T06:45:52Z")

</div>

Hi,  
I need to call a server API to POST some data in custom node's js file. I am trying to use "node-fetch" , but it always gives me " Cannot find module 'node-fetch'" I have installed the module. Module is available under "node\_modules" folder.  
I am using below line of code to test a simple server call:

const fetch = require('node-fetch');  
fetch("some\_url").then(res =\> console.log(res.text()))  
.then(body =\> console.log(body));

Could you please suggest. Also is it a right way to do this?

---

<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:** [10 August 2020 21:25 UTC](https://discourse.nodered.org/t/calling-a-server-api-in-custom-nodes-js/31204/2 "2020-08-10T21:25:04Z")

</div>

> [@Pavithra\_nayak](#):
>
> I have installed the module. Module is available under "node\_modules" folder.

Which folder contains the `node_modules` folder that contains node-fetch?

When posting code, please follow the forum instractions and wrap the code in back-ticks so that it is more readable thanks.

```auto
const fetch = require('node-fetch');
fetch("some_url").then(res => console.log(res.text()))
.then(body => console.log(body));

```

Where are you putting that code? If it is in a function node, you can't use `require()` because a function node uses a Node.js VM which prevents its use.

Instead you have to do the require in your node-red settings.js file assigning the output to the globals section. Then in your function you will have something like:

```auto
const fetch = global.get('fetch');
fetch("some_url").then(res => console.log(res.text()))
.then(body => console.log(body));

```

Where `'fetch'` will be the name you assigned the require() to in settings.js.

---

<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:** [9 October 2020 21:25 UTC](https://discourse.nodered.org/t/calling-a-server-api-in-custom-nodes-js/31204/3 "2020-10-09T21:25:09Z")

</div>

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