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. To do that, pack the prepared node_modules folder and the package.json file into an archive called dependencies.tar.gz. Then add the archive to the widget’s settings. To do that, open the widget in the interface designer, go to the Files tab, and upload the archive file. After that you will be able to use the packed dependencies in the server script code.

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

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

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

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;

Build a package with server dependencies

Shell script (Linux, MacOS)

# Place the script into an empty directory
# Make it executable: $chmod 755 makedeps.sh
# Run it, passing the list of npm packages as parameters, for example: $./makedeps.sh xlsx semver
# A package with dependencies listed in dependencies.tar.gz will be formed 
#
# Docker is required
 
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

PowerShell script (Windows)

# Place the script into an empty directory
# Run it, passing the list of npm packages as parameters, for example: $./makedeps.ps1 xlsx semver
# A package with dependencies listed in dependencies.tar.gz will be formed
#
# Docker is required
 
$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

What you need to know about connecting modules

In a custom script:

  • What can be imported:
    • Modules from package.json ("dependencies" attribute).
  • What cannot be imported:
    • Transitive dependencies.
    • Host modules (in relation to worker).
    • Built-in Node.js modules (fs, path, tty…).

Inside external modules (imported in a script):

  • What can be imported:
    • Any other modules from the directory with the dependencies.
    • Built-in Node.js modules (fs, path, tty…);
  • What cannot be imported:
    • Host modules (in relation to worker).

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