From 434bc01d45e0fb8f1afc6425d0c2801e9c75f603 Mon Sep 17 00:00:00 2001 From: Teppo Kurki Date: Mon, 9 Dec 2024 21:38:35 +0200 Subject: [PATCH] fix: make serialport module optional (#1841) serialport module has been optional, as it is platform specific and its install may fail but otherwise the server would be in working order. However when converting serialports.js to ts using import had made it a hard dependency - the server would not start without it, even if npm install had completed without errors but without serialport still installed. Use dynamic require to load it so that even if the module is missing the server starts. --- src/serialports.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/serialports.ts b/src/serialports.ts index e47edced5..7b191a2ee 100644 --- a/src/serialports.ts +++ b/src/serialports.ts @@ -17,7 +17,6 @@ import { Ports } from '@signalk/server-api' import fs from 'fs' -import { SerialPort } from 'serialport' export const listAllSerialPorts = (): Promise => { return new Promise((resolve, reject) => { @@ -36,8 +35,8 @@ export const listAllSerialPorts = (): Promise => { function listSerialPorts() { try { - // eslint-disable-next-line @typescript-eslint/no-var-requires - return SerialPort.list() + // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports + return require('serialport').SerialPort.list() } catch (_err) { return Promise.resolve([]) }