# Using js file with imported modules in html

**URL:** <https://discourse.nodered.org/t/using-js-file-with-imported-modules-in-html/51165>\
**Category:** Developing Nodes\
**Created:** [17 September 2021 15:02 UTC](https://discourse.nodered.org/t/using-js-file-with-imported-modules-in-html/51165 "2021-09-17T15:02:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![psykoloGG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/psykologg/32/48159_2.png) [@psykoloGG](https://discourse.nodered.org/u/psykoloGG)\
**Post date:** [17 September 2021 15:02 UTC](https://discourse.nodered.org/t/using-js-file-with-imported-modules-in-html/51165/1 "2021-09-17T15:02:04Z")

</div>

In my node's html file I included script file  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/3/73606e2ae188f3a873fd0994ebfc3bad30ac12fb.png)

This file imports/requires npm packages, which I have listed in dependencies and I know they exist.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/a/4ab854c3de8313876970244e98e2df5aa218d2cd.png)

When I try loading this up in Node-RED it give's me error that it cannot find the module.

It also gives some errors about having /, ./ or ../ in the path, but that should not be necessary because it is a global module. If I include ./ws it still throws error.

Any idea on why it is behaving like this? And how shall it be done

---

<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:** [17 September 2021 16:07 UTC](https://discourse.nodered.org/t/using-js-file-with-imported-modules-in-html/51165/2 "2021-09-17T16:07:14Z")

</div>

> [@psykoloGG](#):
>
> In my node's html file

The HTML file runs in the context of your browser and not the Node-RED server - it communicates back to the server via websockets.

So you cannot mix up node.js modules with the html code as that won't ever work.

If you describe what you are trying to achieve, someone may be able to help further.

---

<div class="post-metadata">

**Author:** ![psykoloGG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/psykologg/32/48159_2.png) [@psykoloGG](https://discourse.nodered.org/u/psykoloGG)\
**Post date:** [17 September 2021 16:37 UTC](https://discourse.nodered.org/t/using-js-file-with-imported-modules-in-html/51165/3 "2021-09-17T16:37:23Z")

</div>

Well I am trying to send a websocket or http request from onEditPrepare to alter some properties from server.

---

<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:** [17 September 2021 16:55 UTC](https://discourse.nodered.org/t/using-js-file-with-imported-modules-in-html/51165/4 "2021-09-17T16:55:33Z")

</div>

OK, the normal way to do this is for your node to include a REST API. You can then call that using jQueries helpers from the front end. There is a simple example in the code of the core serial node. More extensive examples are available in the uibuilder node.

[25-serial.js - node-red/node-red-nodes - GitHub1s](https://github1s.com/node-red/node-red-nodes/blob/master/io/serialport/25-serial.js#L489-L499)

[25-serial.html - node-red/node-red-nodes - GitHub1s](https://github1s.com/node-red/node-red-nodes/blob/master/io/serialport/25-serial.html#L339-L352)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [17 September 2021 21:32 UTC](https://discourse.nodered.org/t/using-js-file-with-imported-modules-in-html/51165/5 "2021-09-17T21:32:09Z")

</div>

> [@psykoloGG](#):
>
> send a websocket or http request from onEditPrepare to alter some properties from server

Why are you attempting to affect something server side from the configurations page of your node?

Typically you might request something (like available com ports) but it's not the normal pattern to change something server-side.

---

<div class="post-metadata">

**Author:** ![psykoloGG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/psykologg/32/48159_2.png) [@psykoloGG](https://discourse.nodered.org/u/psykoloGG)\
**Post date:** [17 September 2021 23:31 UTC](https://discourse.nodered.org/t/using-js-file-with-imported-modules-in-html/51165/6 "2021-09-17T23:31:10Z")

</div>

Let me clarify, I am trying to populate a dropdown in node's properties, and I am doing it in onEditPrepare. The data that I want to get has to be parsed from the web, because it changes dynamicaly. So I need to make an update each time when I do onEditPrepare, thus websocket or http call.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 September 2021 16:03 UTC](https://discourse.nodered.org/t/using-js-file-with-imported-modules-in-html/51165/7 "2021-09-18T16:03:06Z")

</div>

In which case can't you use [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to grab this data and process it client side?

---

<div class="post-metadata">

**Author:** ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)\
**Post date:** [18 September 2021 16:20 UTC](https://discourse.nodered.org/t/using-js-file-with-imported-modules-in-html/51165/8 "2021-09-18T16:20:32Z")

</div>

I THINK I know what you are looking for.

> [@psykoloGG](#):
>
> populate a dropdown in node's properties

you mean the config dialog in the Node-RED editor?

This is how I do it (don't know if it's right but works for me)

in your `yourNode.js` inside your main function you establish a listener for http requests

```auto
            RED.httpAdmin.get("/yourNode/"+node.id, RED.auth.needsPermission('your-node.read'), function(req,res) {
                console.log(`http request "${req.query.query}" from ip: ${req.headers.host}`);
                var returnItems = []; // list of Items to send back to the frontend
                var config = req.query;
                switch (config.query) {
                    case 'sendMe':
                        returnItems = ['take this'];
                        break;
                    default:
                        returnItems = `Error! query "${config.query}" unknown`;
                        break;
                }
                res.json(returnItems);
            })

```

and in your `yourNode.html` (where it suites you) you call your listener and get the data in the done callback

```auto
        $.getJSON("/yourNode/"+node.id, {'query': query, 'params': params })
        .done( (data => {console.log('I got this',data)}))
        .fail( function(e1, e2, e3) {
            console.log("Error : " + JSON.stringify(e1));
        });

```

_I pulled the code out of one of my node and simplified it a little bit, hope I made no mistake as I did not tested it_

If there is a better way perhaps someone can correct me 😉

---

<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:** [18 September 2021 16:53 UTC](https://discourse.nodered.org/t/using-js-file-with-imported-modules-in-html/51165/9 "2021-09-18T16:53:42Z")

</div>

> [@Christian-Me](#):
>
> `$.getJSON("/yourNode/`

This is the method used by the Serial node that Julian linked to previously.

But a very important bit is to _not_ start the path with a `/` in the html side. That means it will work even if `httpAdminRoot` has been set to move the editor to a different root URL.

So the above but if code should be:

`$.getJSON("yourNode/`

---

<div class="post-metadata">

**Author:** ![psykoloGG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/psykologg/32/48159_2.png) [@psykoloGG](https://discourse.nodered.org/u/psykoloGG)\
**Post date:** [18 September 2021 17:03 UTC](https://discourse.nodered.org/t/using-js-file-with-imported-modules-in-html/51165/10 "2021-09-18T17:03:58Z")

</div>

I got it working using browserify, not the cleanest solution but it works 😃

---

<div class="post-metadata">

**Author:** ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)\
**Post date:** [18 September 2021 17:54 UTC](https://discourse.nodered.org/t/using-js-file-with-imported-modules-in-html/51165/11 "2021-09-18T17:54:38Z")

</div>

@knolleary, thank you for pointing me to this problem!

@TotallyInformation sorry for not following the provided links. Was reading on my mobile at the beginning.

---

<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:** [2 October 2021 17:55 UTC](https://discourse.nodered.org/t/using-js-file-with-imported-modules-in-html/51165/12 "2021-10-02T17:55:26Z")

</div>

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