Using Node.js Modules in Your Script

Question:

Can I use Node.js modules in NoSQLBooster for MongoDB? I want to use axios in my script.

Answer:

Yes. NoSQLBooster for MongoDB is a Node.js-based desktop application. You can use any Node.js built-in modules (util, fs, path ...) and pure JS NPM packages in NoSQLBooster for MongoDB.


Current Node.js Version in NoSQLBooster for MongoDB

console.log(process.versions.node); //return:  22.x

Use Node.js built-in modules

There is no extra step to use Node.js built-in modules, Just import/require the module and use it in your script.

const {existsSync}=require("fs"); //CommonJS module
existsSync("myFile");

const path=require("path");
path.resolve('/foo/bar', './baz');// Returns: '/foo/bar/baz'

Use NPM Packages

To install and use NPM Packages in NoSQLBooster for MongoDB, please follow the steps. You don't need to install Node.js modules globally.

  1. Launch NoSQLBooster for MongoDB.
  2. Execute Main Menu -> Help -> Open the Executable Directory (or "Open User Data Directory" in the old version)
  3. New Terminal at this folder

Before you can start using npm, first, you have to install Node.js on your system.

npm i axios  # run it in NoSQLBooster for MongoDB user data directory

After successfully installing this package in the NoSQLBooster for MongoDB User Data Directory, you can require and access it in the NoSQLBooster for MongoDB script.

const axios=require("axios");
const rst = await axios.get('https://api.github.com/users/github'); // top-level await
console.log(rst.data);

NoSQLBooster 11.0 embeds the mongosh engine which supports native top-level await. You can use the standard await keyword directly at the top level of your scripts. Shell-api methods like find(), insertOne(), aggregate() are auto-resolved and do not need await.

Run it and got a result.

{
    "avatar_url" : "https://avatars2.githubusercontent.com/u/9919?v=3",
    "bio" : "How people build software.",
    "blog" : "https://github.com/about",
    "company" : null,
    "created_at" : "2008-05-11T04:37:31Z",
 ...
 ...
}

Now, you can assemble NPM packages like building blocks in your MongoDB shell script. The npm registry hosts almost half a million packages of free, reusable code — the largest software registry in the world.


Use your own javascript files

NoSQLBooster supports CommonJS module, each file is treated as a separate module. For example, consider a file named "c://test/myproject/utils.js"

//c://test/myproject/utils.js

const replaceStr = (str, char, replacer) => {
  const regex = new RegExp(char, "g")
  const replaced = str.replace(regex, replacer)
  return replaced
}
exports.replaceStr =  replaceStr;

then import "c://test/myproject/utils.js" in your script.

const {replaceStr}= require("c://test/myproject/utils");
console.log(replaceStr("hello world","world","nosqlbooster"));  //hello nosqlbooster