How to import JavaScript Module in Resource Extension in MarkLogic 10

Viewed 145

MarkLogic v10 introduced the support for native JavaScript Module (ECMAScript module).

https://docs.marklogic.com/guide/jsref/modules

However I am not sure how to import such modules in resource extensions. For example:

'use strict';

import * as jsearch = from '/MarkLogic/jsearch.mjs';

function get(context, params) {
  // omitted
};

exports.GET = get;

would lead to the following error:

JS-JAVASCRIPT: import * as jsearch = from '/MarkLogic/jsearch.mjs'; -- Error running JavaScript request: SyntaxError: Unexpected token *;

Similarly, importing a custom module such as import { foo } from '/bar.mjs' results in a similar error: Unexpected token {.

Thank you!

1 Answers

You have a small syntax error. Remove the = from your import statement:

import * as jsearch from '/MarkLogic/jsearch.mjs';

However, it looks as if you are building a custom REST extension. Custom REST extensions cannot use mjs modules. Because resource service extensions are imported dynamically by the REST API for the current request, the REST API cannot get the benefit of statically cached JavaScript *.mjs module libraries.

You might consider exposing the functionality via Data Services, which can import mjs modules. Check out the Creating Data Services and Developer Actions in Node.js for further information.

Related