Function node can't use mongodb module


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:

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

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"


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

const { MongoClient } = require('mongodb')

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

const mongodb = require('mongodb')

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

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.

1 Like

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

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

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.

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

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