# Uncaught ReferenceError: require is not defined

**URL:** <https://discourse.nodered.org/t/uncaught-referenceerror-require-is-not-defined/37868>\
**Category:** Core Development\
**Created:** [22 December 2020 08:17 UTC](https://discourse.nodered.org/t/uncaught-referenceerror-require-is-not-defined/37868 "2020-12-22T08:17:13Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![GowriAradhya](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gowriaradhya/32/25062_2.png) [@GowriAradhya](https://discourse.nodered.org/u/GowriAradhya)\
**Post date:** [22 December 2020 08:17 UTC](https://discourse.nodered.org/t/uncaught-referenceerror-require-is-not-defined/37868/1 "2020-12-22T08:17:14Z")

</div>

How to install **require** and **CryptoJS** module in nodered??

When starting node-red i am not getting any errors. While launching i am getting  
Uncaught ReferenceError: require is not defined in the console.

I tried to install require and CryptoJS by the following command.  
npm install require  
npm install crypto-js and it is added in the **package.json** of nodered .

---

<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:** [22 December 2020 08:21 UTC](https://discourse.nodered.org/t/uncaught-referenceerror-require-is-not-defined/37868/2 "2020-12-22T08:21:31Z")

</div>

> [@GowriAradhya](#):
>
> I tried to install require and CryptoJS by the following command.

short answer - these are not node-red nodes.  
longer answer - what is it you are trying to achieve? there may already be proper node-red nodes ready for use.

---

<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:** [22 December 2020 08:26 UTC](https://discourse.nodered.org/t/uncaught-referenceerror-require-is-not-defined/37868/3 "2020-12-22T08:26:21Z")

</div>

If you are trying to use `require` inside a Function node, you can't.

The docs explain how to add additional modules to the function node:

> **[Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules)**

---

<div class="post-metadata">

**Author:** ![GowriAradhya](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gowriaradhya/32/25062_2.png) [@GowriAradhya](https://discourse.nodered.org/u/GowriAradhya)\
**Post date:** [22 December 2020 08:34 UTC](https://discourse.nodered.org/t/uncaught-referenceerror-require-is-not-defined/37868/4 "2020-12-22T08:34:16Z")

</div>

For Nodered user Login (Custom user authentication) . we are trying to encrypt the password from client side before sending to server. So we are trying to install require module.

---

<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:** [22 December 2020 08:40 UTC](https://discourse.nodered.org/t/uncaught-referenceerror-require-is-not-defined/37868/5 "2020-12-22T08:40:55Z")

</div>

Hmmm I guessed you might be on the wrong road. So this error...

> [@GowriAradhya](#):
>
> Uncaught ReferenceError: require is not defined in the console.

... is client side right?

Installing node-modules in node-red does not automatically present them to the client.

Unfortunately, I have never implemented custom user auth for node-red - hopefully someone else can chime in.

---

<div class="post-metadata">

**Author:** ![GowriAradhya](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gowriaradhya/32/25062_2.png) [@GowriAradhya](https://discourse.nodered.org/u/GowriAradhya)\
**Post date:** [22 December 2020 08:51 UTC](https://discourse.nodered.org/t/uncaught-referenceerror-require-is-not-defined/37868/6 "2020-12-22T08:51:27Z")

</div>

In this particular file node-red/packages/node\_modules/@node-red/editor-client/src/js/user.js  
before sending ajax post call $.ajax({  
url: "auth/token",  
type: "POST",  
data: body  
})  
We are trying to encrypt the password using Crpyto.js library.

---

<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:** [22 December 2020 08:58 UTC](https://discourse.nodered.org/t/uncaught-referenceerror-require-is-not-defined/37868/7 "2020-12-22T08:58:44Z")

</div>

Again, I am no expert here, but I believe (typically) most folk just rely on HTTPS to encrypt the data from client to server (and/or use something like nginx)

Are you using https?

* * *

PS, as you are modifying the "backend" (core of node-red), is it your intension to maintain your own fork & keep it up to date with all the future improvements made by the node-red devs?  
**or**  
are you intending on improving the node-red core and issuing a Pull Req to node-red so that your modifications are built in? If so, you would need to discuss with maintainers first.

* * *

PS2: You seem to be veering off from the solution that Nick pointed you towards [back in July](https://discourse.nodered.org/t/how-to-write-and-configure-custom-authentication/29381). Is there an issue with the documentation he provided?

---

<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:** [22 December 2020 13:49 UTC](https://discourse.nodered.org/t/uncaught-referenceerror-require-is-not-defined/37868/8 "2020-12-22T13:49:09Z")

</div>

Just to get this straight - you are trying to authenticate your users before they are allowed access to the Node-RED Editor? Or is it access to a web page served by Node-RED?

In either case, trying to encrypt in the browser is going to be the wrong approach. Encryption is generally reversible and doing it in the client creates all manner of security vulnerabilities.

You can cryptographically **hash** values in the client because that can't be reversed and it can be useful to hash a password before sending it to the server.

However, you still need to use TLS to give wire-level encryption from client to server. As Colin says, that can either be done using Node-RED itself or (probably more scalable) using a reverse proxy to terminate the TLS.

Using something like NGINX, IIS, Apache or whatever also lets you add user authentication (and even authorisation) using the proxy. This is generally easier to manage securely than trying to squeeze everything into Node-RED (though it can do it as well if needed).

**ALWAYS** use standards-based authentication and authorisation methods - **NEVER** roll your own as you seem to be trying to do.

* * *

All of the above is friendly advice, not professional guidance.

Above all else, if you need security and you aren't sure how to do it - get a professional - at least get your work tested by professionals.

Security is hard to get right but _very_ easy to mess up.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 December 2020 14:39 UTC](https://discourse.nodered.org/t/uncaught-referenceerror-require-is-not-defined/37868/9 "2020-12-22T14:39:35Z")

</div>

> [@TotallyInformation](#):
>
> As Colin says

Happy as I am to bask in Steve's glory, my conscience forces me to point out that it was himself not me.

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [22 December 2020 16:11 UTC](https://discourse.nodered.org/t/uncaught-referenceerror-require-is-not-defined/37868/10 "2020-12-22T16:11:00Z")

</div>

> [@Steve-Mcl](#):
>
> PS2: You seem to be veering off from the solution that Nick pointed you towards [back in July](https://discourse.nodered.org/t/how-to-write-and-configure-custom-authentication/29381). Is there an issue with the documentation he provided?

Thanks for that link to the @knolleary's link. Over the last week, I was working on a custom authentication system to protect my dashboard and mp4 video routes and was ready to move to the step of using my middleware to protect the admin section. I am not very good at reading the docs, apparently. 🤓

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [22 December 2020 16:44 UTC](https://discourse.nodered.org/t/uncaught-referenceerror-require-is-not-defined/37868/11 "2020-12-22T16:44:25Z")

</div>

It seems that `httpAdminMiddleware` can only receive a single function and does not support receiving an array of functions, similar to the limitation of `node-red-dashboard`. For the meanwhile, I will rewrite my middleware to be a single function to be compatible.

---

<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 February 2021 16:44 UTC](https://discourse.nodered.org/t/uncaught-referenceerror-require-is-not-defined/37868/12 "2021-02-20T16:44:45Z")

</div>

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