# Function node can't use mongodb module

**URL:** <https://discourse.nodered.org/t/function-node-cant-use-mongodb-module/78617>\
**Category:** General\
**Created:** [22 May 2023 17:18 UTC](https://discourse.nodered.org/t/function-node-cant-use-mongodb-module/78617 "2023-05-22T17:18:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [22 May 2023 17:18 UTC](https://discourse.nodered.org/t/function-node-cant-use-mongodb-module/78617/1 "2023-05-22T17:18:55Z")

</div>

Hello

I have to access a MongoDB with a database that is dynamically defined.  
I did install the node-red-node-mongodb but I see this is not possible as the database is static inside the configuration.

So I'm trying to recode that myself into a function node (but I'm far from being a JS expert).

In the function node, I added the `mongodb` module:

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

But when I deploy (without having coded anything) I get that error message:

```auto
Error: Cannot find module '/data/node_modules/mongodb/index.js'. Please verify that the package.json has a valid "main" entry

```

Actually `/data/node_modules/mongodb/package.json` has `"main": "lib/index.js"`

Could anyone advise me what could be missing ?

I am surprised because I already did something similar to use `aws-sdk` with no problems and the `package.json` of `aws-sdk` has `"main": "lib/aws.js"`  
So I don't understand why for mongodb it search the "main" in the main folder and not in "lib" while it works well with "aws-sdk"

Thanks

---

<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 May 2023 19:55 UTC](https://discourse.nodered.org/t/function-node-cant-use-mongodb-module/78617/2 "2023-05-22T19:55:26Z")

</div>

> [@Barbudor](#):
>
> `mongodb`

Lookst like the recommended way to use that library starts like this:

```auto
const { MongoClient } = require('mongodb')

```

But when you use the module manager in Node-RED's function node, you get the equivalent of this:

```auto
const mongodb = require('mongodb')

```

Which means that, to access the client, in your function code, you would do this:

```auto
const MongoClient = mongodb.MongoClient

```

Then you should be able to follow the example from the module's readme.

PS: Just to be clear, you don't need any require statements since the function node's module manager does that for you.

---

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [22 May 2023 21:14 UTC](https://discourse.nodered.org/t/function-node-cant-use-mongodb-module/78617/3 "2023-05-22T21:14:41Z")

</div>

Hi @TotallyInformation

Thanks but the problem is that I get the above error before adding any code in OnStart/OnMessage  
I haven't tried to call MongoClient yet.  
I just declared mongodb in the Setup tab of the function node.

If I do like you say without the module declared in Setup, then I get a "ReferenceError: mongodb is not defined (line 1, col 21)"

If I add mongodb to Setup, I'm back to my initial problem

---

<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 May 2023 22:11 UTC](https://discourse.nodered.org/t/function-node-cant-use-mongodb-module/78617/4 "2023-05-22T22:11:16Z")

</div>

> [@Barbudor](#):
>
> `mongodb`

So I tried to install the module and I get this:

```auto
0|Node-RED | 22 May 23:02:27 - [info] Installing module: mongodb, version: latest
0|Node-RED | 22 May 23:02:49 - [error] Installation of module mongodb failed:
0|Node-RED | 22 May 23:02:49 - [error] ------------------------------------------
0|Node-RED | 22 May 23:02:49 - [error] npm WARN config production Use `--omit=dev` instead.
0|Node-RED | npm ERR! code EBADENGINE
0|Node-RED | npm ERR! engine Unsupported engine
0|Node-RED | npm ERR! engine Not compatible with your version of node/npm: eslint-plugin-jsdoc@36.0.8
0|Node-RED | npm ERR! notsup Not compatible with your version of node/npm: eslint-plugin-jsdoc@36.0.8
0|Node-RED | npm ERR! notsup Required: {"node":"^12 || ^14 || ^16"}
0|Node-RED | npm ERR! notsup Actual: {"npm":"9.5.0","node":"v18.15.0"}
0|Node-RED |
0|Node-RED | npm ERR! A complete log of this run can be found in:
0|Node-RED | npm ERR! C:\Users\julia\AppData\Local\npm-cache\_logs\2023-05-22T22_02_30_709Z-debug-0.log
0|Node-RED |
0|Node-RED | 22 May 23:02:49 - [error] ------------------------------------------
0|Node-RED | 22 May 23:02:49 - [info] Failed to load external modules required by this flow:
0|Node-RED | 22 May 23:02:49 - [info] - mongodb [unexpected_error]

```

Which reminded me of why I ditched MongoDB in the first place. Their weird approach to support. Node.js v18 has been live since April last year!

Both the devices where I run Node-RED have node.js v18 so I can't help further I'm afraid.

---

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [23 May 2023 08:16 UTC](https://discourse.nodered.org/t/function-node-cant-use-mongodb-module/78617/5 "2023-05-23T08:16:24Z")

</div>

Hi @TotallyInformation

Many thanks a lot for trying and helping.

Actually I'm running docker nodered-3.0.2-latest which is nodered-3.0.2-16, just because "latest" is the default one. So I believe I will soon be hit by the same problem when latest evolve to nodejs 18.

Thanks a lot

---

<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:** [22 July 2023 08:16 UTC](https://discourse.nodered.org/t/function-node-cant-use-mongodb-module/78617/6 "2023-07-22T08:16:43Z")

</div>

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