Search code examples
javascriptmarklogicserverside-javascript

Importing JS modules from MarkLogic console


(Forgive me for terminological mistakes, as I am new to JS and MarkLogic, but DO correct me if I am wrong somewhere.)

I want to import a BigNumber object (or is it a class, construct..) from external module bignumber.js using MarkLogic qconsole (Server side JS).

This is the suggested way from their github, but it is meant for Node.js and not for SSJS. https://github.com/MikeMcl/bignumber.js/

const BigNumber = require('bignumber.js');
import BigNumber from "bignumber.js";
import { BigNumber } from "bignumber.js";

My setup look like this so far:

I have opened HTTP Server

  • port: 8111
  • root: /
  • modules: my-db-modules
  • database my-db
  • default user: admin

I inserted bignumber.js document into my-db-modules with the following lines:

declareUpdate();
xdmp.documentLoad('path/bignumber.js', {'uri': 'bignumber.js'});

I have inserted bignumber.mjs file as well.

When I try to import BigNumber from .js file I get the 'Module bignumber.js not found' and if I try importing it from .mjs file I get the 'Document is not of executable mimetype. URI: bignumber.mjs'

I found this section on how to use module db: https://docs.marklogic.com/guide/admin/databases#id_38484

But I don't know if I got it right, because I experimented with changing the root of my HTTP Server to http://marklogic.com/ with no results.


Solution

  • As MadsHansen pointed out in the comment - document should be inserted with a leading slash in its name.

    I just wanted to clarify this next part:

    • Importing with require works with .js files
    const BigNumber = require('/bignumber.js');
    
    • This style of import works with .mjs files
    import BigNumber from "/bignumber.mjs";
    import { BigNumber } from "/bignumber.mjs";
    

    Both .js and .mjs files shoud be inserted into modules db, and used in main db.

    I loaded files with these lines:

    xdmp.documentLoad('path/bignumber.js', {'uri': '/bignumber.js'});
    xdmp.documentLoad('path/bignumber.mjs', {'uri': '/bignumber.mjs'});