1

I'm running a Marklogic server with an exposed REST API. I have extended the API with my own resources. These endpoints are just .sjs files as dictated by the documentation. Is it possible to use regular node modules (installed via npm) in these sjs files?

I'm using ml-gradle to deploy the server and endpoints. I'm not which directory I would install the npm modules.

I try doing an npm install and then importing it with a relative path

const lodash = require('../node_modules/lodash') 

However, my sjs file can never find those modules. I'm assuming the import path I give it is not correct.

user2183384
  • 101
  • 1
  • 10

1 Answers1

3

MarkLogic server doesn't use Node.js and thus isn't aware when packages are installed into a Node.js instance.

Instead, the JavaScript files for the library have to be installed in the modules database for the REST API instance. See:

http://docs.marklogic.com/guide/node-dev/extensions

One general caution about npm packages...

Node.js and SJS (Server-Side-JavaScript) in MarkLogic are fundamentally different environments:

  • Node.js is single-threaded and event driven, providing a global state that is durable for the life of the Node.js process.

  • SJS is concurrent and transactional, providing a separate state for each main module that is durable for the life of a single synchronous invocation of that main module.

As a consequence, evented packages aren't useful in the SJS environment.

However, JavaScript libraries (including lodash as well as many parsers) that provide functions that run to completion without eventing are usable in the SJS environment.

See also:

http://docs.marklogic.com/guide/jsref/language#id_67900

On the particular question, some people feel that lodash has been largely superseded by ES6, which SJS supports.

Hoping that helps,

ehennum
  • 7,295
  • 13
  • 9