Using node modules in MongoBooster

Question:

Can I use node modules in MongoBooster? I want to use axios in my script. And, Do I have to install node module globally?

Answer:

Yes, You can use pure JS node modules in MongoBooster. And, you don't need to install node modules globally.

  1. Launch MongoBooster.
  2. Execute Main Menu -> Help -> Open the Executable Directory (or "Open User Data Directory" in the old version)
  3. New Terminal at this folder
1
npm i axios  # run it in MongoBooster user data directory

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

1
2
3
const axios=require("axios");
let rst=await (axios.get('https://api.github.com/users/github'));//await promise The "await" is a build-in method in MongoBooster. refer to: https://www.mongobooster.com/blog/using-functions-with-async-callback
console.log(rst.data);

The "await" is a build-in method in MongoBooster. You can use it to await a promise or a promise array.

Run it and got result.

1
2
3
4
5
6
7
8
9
{
"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",
...
...
}

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"

1
2
3
4
5
6
7
8
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.

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

Now, you can assemble 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.

Thank you!

Please visit our feedback page or click the “Feedback” button in the app. Feel free to suggest improvements to our product or service. Users can discuss your suggestion and vote for and against it. We’ll look at it too.