# Noob question re: using a Node.js script in a function node

**URL:** <https://discourse.nodered.org/t/noob-question-re-using-a-node-js-script-in-a-function-node/88535>\
**Category:** General\
**Created:** [6 June 2024 12:12 UTC](https://discourse.nodered.org/t/noob-question-re-using-a-node-js-script-in-a-function-node/88535 "2024-06-06T12:12:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [6 June 2024 12:12 UTC](https://discourse.nodered.org/t/noob-question-re-using-a-node-js-script-in-a-function-node/88535/1 "2024-06-06T12:12:44Z")

</div>

Hello. I would like to run this Node.js script in a function node (used in [this article](https://bootcamp.uxdesign.cc/openais-image-vision-api-seeing-is-believing-and-it-s-unbelievably-cool-5b959e9d0170)).

```auto
import OpenAI from "openai";

const openai = new OpenAI({apiKey: '...'});

async function main() {
  const response = await openai.chat.completions.create({
    model: "gpt-4-vision-preview",
    messages: [
      {
        role: "user",
        content: [
          { type: "text", text: "What’s in this image?" },
          {
            type: "image_url",
            image_url:
              "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
          },
        ],
      },
    ],
  });
  console.log(response.choices[0]);
}
main();

```

I copied the above (and inserted my API key) and put `return msg;` as the last line of code.

I then wired the output of the above function node to an http request node with these settings

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

(seems odd to have to pass my API key again as bearer authentication, but am pretty sure that URL requires it).

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

Doing the above yields nothing in the debug screen. I also wired a separate debug node after the function node and still get nothing, indicating my function node is not doing anything. I have a feeling that I am missing something obvious here. Should the above node.js script work in Node-RED, or do I need to do something else?

Here is the URL that I was trying to use:

```auto
https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg

```

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [6 June 2024 13:03 UTC](https://discourse.nodered.org/t/noob-question-re-using-a-node-js-script-in-a-function-node/88535/2 "2024-06-06T13:03:54Z")

</div>

Hi @grant1

The Node Red `function` Node requires some more involvement, then just a simple copy and paste.

1. You cant `import` directly in the script
2. If any script your using, is based on an `async` response, you can't simply `return msg`

To address the above you need to.

1. Register the 3rd party library in the `function` node settings
2. Use `node.send` instead of `return msg` to support `async`

See the import below, to start you off (pay attention to the `Setup` tab)

```auto
[{"id":"b47ddfdc7da359a6","type":"function","z":"197c16697aae09bd","name":"Open AI","func":"const OAI = new openai.OpenAI({ apiKey: '...' });\n\nasync function main() {\n const response = await OAI.chat.completions.create({\n model: \"gpt-4-vision-preview\",\n messages: [\n {\n role: \"user\",\n content: [\n { type: \"text\", text: \"What’s in this image?\" },\n {\n type: \"image_url\",\n image_url:\n \"https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg\",\n },\n],\n },\n ],\n });\n node.send({ payload: response.choices[0] })\n}\nmain();","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"openai","module":"openai"}],"x":500,"y":360,"wires":[["0a281ed001c5fb67"]]},{"id":"756d370590baf3ca","type":"inject","z":"197c16697aae09bd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":360,"wires":[["b47ddfdc7da359a6"]]},{"id":"0a281ed001c5fb67","type":"debug","z":"197c16697aae09bd","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":700,"y":360,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [6 June 2024 13:13 UTC](https://discourse.nodered.org/t/noob-question-re-using-a-node-js-script-in-a-function-node/88535/3 "2024-06-06T13:13:30Z")

</div>

Thank you! Perhaps a year ago I was messing with a nodejs script and I had forgotten about registering the 3rd party library in the Setup tab.

Since I made my original post, I came up with another way to accomlish the same thing by injecting the JSON data like this, but using the function node is way better.

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

---

<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:** [20 June 2024 13:14 UTC](https://discourse.nodered.org/t/noob-question-re-using-a-node-js-script-in-a-function-node/88535/4 "2024-06-20T13:14:28Z")

</div>

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