Build solutions in ELMA365 / Server dependencies in npm packages

Server dependencies in npm packages

In ELMA365 On-Premises, you can add server dependencies in widget scripts. This allows you to solve some uncommon problems without writing custom microservices. Instead, you can use the rich ecosystem of npm packages when writing server scripts. For example, you can add the XLSX package to read data from Excel files using the following commands:

import * as XLSX from 'xlsx/xlsx.mjs';
 
const url = await Context.data.file.getDownloadUrl();
const req = await fetch(url);
const data = await req.arrayBuffer();
 
let workbook = XLSX.read(data);
let cell_value = workbook['Sheet 1']['A1'].v;

начало внимание

Server dependencies in npm packages can be added only in ELMA365 On-Premises.

конец внимание

To create an npm package, pack the prepared node_modules folder and the package.json file into an archive called dependencies.tar.gz. For more details see:

Install Docker before starting work.

Building a package with server dependencies using a Shell script for Linux, macOS

  1. Create an empty folder on your computer. Copy the script below and save it as a file named makedeps.sh in the created folde:

rm -rf ./deps
docker run -v `pwd`:/data -w /data/deps --platform linux/amd64 --rm node:14.16.0-alpine /bin/sh -c "npm init -y && npm i $*"
docker run -v `pwd`:/data -w /data --platform linux/amd64 --rm busybox tar -zcf dependencies.tar.gz -C /data/deps node_modules package.json
rm -rf ./deps

  1. Through the developer console, find the saved file and make it executable:

$chmod 755 makedeps.sh

  1. Run the script, passing a list of npm packages as parameters. For example, for the XLSX package:

$./makedeps.sh xlsx semver

As a result of the command execution, an archive with dependencies named dependencies.tar.gz.

  1. Upload the package to the system; this can be done in the widget settings in the interface designer and in the custom module when creating an API method: add the obtained dependencies archive on the Files tab.

Packed server dependencies can be used in server script code.

Building a package with server dependencies using a PowerShell script for Windows

  1. Create an empty folder on your computer. Copy the script below and save it as a file named makedeps.ps1 in the created folder:

$ErrorActionPreference = "SilentlyContinue" #This will hide errors
Remove-Item -Recurse -Force .\deps
$ErrorActionPreference = "Continue" #Turning errors back on
docker run -v ${PWD}:/data -w /data/deps --platform linux/amd64 --rm node:14.16.0-alpine /bin/sh -c "npm init -y && npm i $args"
docker run -v ${PWD}:/data -w /data --platform linux/amd64 --rm busybox:stable tar -zcf dependencies.tar.gz -C /data/deps node_modules package.json
Remove-Item -Recurse -Force .\deps

  1. Run the script, passing a list of npm packages as parameters. For example, for the XLSX package:

$./makedeps.ps1 xlsx semver

As a result of the command execution, an archive with dependencies named dependencies.tar.gz.

  1. Upload the package to the system; this can be done in the widget settings in the interface designer and in the custom module when creating an API method: add the obtained dependencies archive on the Files tab.

Packed server dependencies can be used in server script code.

Module connection features

Pay attention to some features of importing components when connecting modules.

In custom scripts

can be imported:

cannot be imported:

  • modules from package.json (dependencies section).
  • transitive dependencies;
  • host modules (in relation to worker);
  • built-in node.js modules (fs, path, tty, etc.).

In external modules imported into the script

can be imported:

cannot be imported:

  • any other modules present in the dependencies directory.;
  • built-in node.js modules (fs, path, tty, etc.).
  • модули хоста (по отношению к worker).

TypeScript compilation on the server is performed with the following tsconfig parameters:

{
    allowJs: true,
    checkJs: false,
    declaration: false,
    module: "commonjs",
    moduleResolution: "node",
    noImplicitAny: true,
    strictNullChecks: true,
    stripInternal: true,
    skipDefaultLibCheck: true,
    target: "ES2017",
    typeRoots: ['node_modules/@types', 'node_modules'],
    lib: ['es2016']
}

Note that not all npm packages work with these parameters without modifications. You may need to wrap them in a thin layer compatible with CommonJS or AMD.

Found a typo? Highlight the text, press ctrl + enter and notify us