Skip to content

Commit

Permalink
fix: make serialport module optional (#1841)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tkurki authored Dec 9, 2024
1 parent 8c80e65 commit 434bc01
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/serialports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import { Ports } from '@signalk/server-api'
import fs from 'fs'
import { SerialPort } from 'serialport'

export const listAllSerialPorts = (): Promise<Ports> => {
return new Promise((resolve, reject) => {
Expand All @@ -36,8 +35,8 @@ export const listAllSerialPorts = (): Promise<Ports> => {

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([])
}
Expand Down

0 comments on commit 434bc01

Please sign in to comment.