# Using modules with nodejs separate from NR?

**URL:** https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019
**Category:** General
**Created:** [1 August 2020 19:31 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019 "2020-08-01T19:31:36Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [1 August 2020 19:31 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/1 "2020-08-01T19:31:37Z")

</div>

In a different directory than NR, I tried installing a node module, separate from NR, # npm install . I got module not found using a require(''). So I did an uninstall, then global install of the module, # npm install -g . This resulted in EACCESS errors. What am I missing, are global install of modules not seen by node js? This lead me to the following qualification, [https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally](https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally).

Is any of the above doing to break NR? Is NR variant of npm not installed with a version manager for npm?

---

<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: [1 August 2020 19:50 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/2 "2020-08-01T19:50:10Z")

</div>

> [@Nodi.Rubrum](#):
>
> Is NR variant of npm not installed with a version manager for npm?

There is no such thing as a node-red variant of npm. You have whatever version of npm you have. If you're using the Pi install script we provide, you'll have got whatever version of npm is packaged with the version of node.js that was installed.

> [@Nodi.Rubrum](#):
>
> In a different directory than NR, I tried installing a node module, separate from NR, # npm install . I got module not found using a require('').

Where was the code (relative to directory you ran the npm install in) that tried to do the require?

> [@Nodi.Rubrum](#):
>
> So I did an uninstall, then global install of the module, # npm install -g . This resulted in EACCESS errors.

EACCESS means the process didn't have permission to write the files. This is because the global module location is typically somewhere that requires root access.

This article gives a complete explanation for how node tries to find something when you `require` it - [Requiring modules in Node.js: Everything you need to know](https://www.freecodecamp.org/news/requiring-modules-in-node-js-everything-you-need-to-know-e7fbd119be8/)

---

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [1 August 2020 20:21 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/3 "2020-08-01T20:21:57Z")

</div>

So node\_modules under .node-red is a local repo, right? Modules in that 'dir tree' would be invisible to a require 'call' from some other directory not under .node-red, say /root for example, right? Thus npm run under .node-red is in 'effect' a local scope for npm. Especially if you do not use '-g' for npm installs in that directory. The npm command may be scope less, but what actions you take and where with npm do... in fact create a scope. This why you can end up with 'nobody' ownership, why you can end up with EACCESS errors. Right?

---

<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: [1 August 2020 20:58 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/4 "2020-08-01T20:58:52Z")

</div>

npm (not -g) installs to the current directory (ie node\_modules is in the current directory). npm -g installs to a system location and on Linux will generally require sudo to write there, that is why you got the access permission error (you didn't use sudo). On linux generally it is either `sudo npm -g install` or `npm install` (no sudo, no -g).

Globally installed nodes will be available from anywhere in the system, locally installed ones are available only from the directory where they were installed.

---

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [2 August 2020 00:10 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/5 "2020-08-02T00:10:40Z")

</div>

Cool. Thanks for details. Just seemed odd a module installed with -g using root, could get an EACCESS error.

---

<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: [2 August 2020 00:36 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/6 "2020-08-02T00:36:46Z")

</div>

This is good not odd. Global installs need root to install but not necessarily to run. That's because of the fact that global installs need write access to system folders. Read/execute access is then available to all.

So global installs are protected from change by ordinary users.

In general though, global installs of npm modules should be avoided since they can introduce unexpected security issues.

---

<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: [2 August 2020 07:25 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/7 "2020-08-02T07:25:27Z")

</div>

> [@Nodi.Rubrum](#):
>
> Just seemed odd a module installed with -g using root, could get an EACCESS error.

I had missed the fact that you said you ran npm as root. Can you repeat that and post the resulting output please?

---

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [4 August 2020 01:36 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/8 "2020-08-04T01:36:00Z")

</div>

Rats... I reset the environment.. If I get the chance will try to recreate the scenario.

Moreover, I still see module not found issues for odd reasons in just using node. Google is just buried in issues and possible fixes to when or how node modules are not found, the prefix is wrong, or the NODE\_PATH variable not set, the list of potential pit falls is extensive.

I have for now, just restored to using absolute paths. This is not a NR issue, as long as NR modules are placed under .node-red/node\_modules, no issues.

It is when I am writing code under just node to get i2c, spi, etc. communication right, or code logic finalized, and once the code is solid, only then dropping it in or wrapping it up for NR.

I am developing a true love/hate relationship with npm.

---

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [4 August 2020 01:51 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/9 "2020-08-04T01:51:46Z")

</div>

Another question... is there a way to programmatically determine if code is running under NR versus just node? It would be cool if I could use console.log() versus node.log() at the appropriate time... using just node in writing the bulk of the code logic, for me, is faster than loading it into NR over and over.

---

<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: [4 August 2020 06:54 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/10 "2020-08-04T06:54:36Z")

</div>

Probably yes but a simple way would be to use an env variable e.g....

`process.env.WITHOUTNODERED`

If you determine you are NOT in node-red, then you could set `node.xxxx` functions to proxy functions instead of littering your code with ` if(isNodeRed) node.log(X) else console.log(X)`

More info on env vars...

> **[Working with Environment Variables in Node.js](https://www.twilio.com/blog/working-with-environment-variables-in-node-js-html)**
>
> Environment variables are a great way to configure parts of your Node.js application. Learn how to work with them and some tools that make your life easier.

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [4 August 2020 10:09 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/11 "2020-08-04T10:09:07Z")

</div>

Maybe it could simply be declaring:

```auto
var node = node ? node : {
  log: console.log,
  error: console.error,
  warn: console.warn,
  debug: console.debug,
  trace: console.trace
};

```

The unneeded log functions could be left out and that made into a simple one liner:

```auto
var node = node ? node : { log: console.log, error: console.error, warn: console.warn };

```

This seemed to work both on node and a function node due to using `var`. With `const` I got an error on the function node as `node` is already declared.

---

<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: [4 August 2020 10:27 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/12 "2020-08-04T10:27:38Z")

</div>

As I typed that from my mobile i thought it easier than asking if `node` was defined as often `var node = this` is written in custom nodes - so calling `var node = node ? node : { log: console.log, error: console.error, warn: console.warn };` may have failed due to `node === this` regardless of whatever `this` is.

_(few, hope that ^ made sense!)_

I dont have time to run up a custom node-red contrib node and find a simple means of understanding if I took the red pill or blue pill so suggested a sure fire way of knowing via an explicit env var (if you set it - then its true right!).

PS, I'm bookmarking that nice snippet (because I always forget how that `var xxx = { yyy, zzz}` syntax works - just cant seem to commit it to memory (I used to struggle with arrow functions syntax too 😉 !)

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [4 August 2020 10:29 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/13 "2020-08-04T10:29:40Z")

</div>

Yep don't know about custom nodes, this was only for function nodes and plain CLI node.js.

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [4 August 2020 10:31 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/14 "2020-08-04T10:31:47Z")

</div>

Actually for this use case this could be further simplified to just:

```auto
var node = node ? node : console;

```

Naturally you'd need to be careful not to use any `node` or `console` specific functions to maintain compatibility both ways.

---

<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: [4 August 2020 11:46 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/15 "2020-08-04T11:46:10Z")

</div>

> [@Nodi.Rubrum](#):
>
> I am developing a true love/hate relationship with npm

Ah yes, anyone who has to wrangle with npm eventually ends up in that place!

> [@Nodi.Rubrum](#):
>
> is there a way to programmatically determine if code is running under NR

There are many ways and it somewhat depends where you want to determine that. In most cases, if the object `RED` exists, you can be pretty sure that you are running under Node-RED. But of course, there are places in code when that won't be available. and in that case, you can use node.js's `process` module to find out what executable started the current process.

---

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [4 August 2020 13:48 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/16 "2020-08-04T13:48:52Z")

</div>

This is just to allow easy code development, debugging, so does not need to be complex or robust, the console references would be stripped from code as development cycle comes to a close. I typically have a helper function that does exception/status reporting, so easy to check each time I would call .log,, .debug, etc.

---

<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: [3 October 2020 13:48 UTC](https://discourse.nodered.org/t/using-modules-with-nodejs-separate-from-nr/31019/17 "2020-10-03T13:48:54Z")

</div>

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