typescriptnpmdenonode-serialport

How to use `serialport` library with `deno`?


while experimenting with deno to get readings from arduino, i got stuck on proper usage of serialport lib. i tried the following:


according to an article packages from pika.dev can be used. but from pika i get:
However, no web-optimized "module" entrypoint was found in its package.json manifest.


also using jpsm.org didn't work either:

import * as SerialPort from "https://dev.jspm.io/serialport";

breaks with:
error: Uncaught TypeError: exists is not a function
probably because of the missing type headers that should be available with npm as @types/serialport but not available on pika.dev or jspm.io.


using npm to install serialport and @types/serialport and referring to their local path as following, wasn't the solution either:

// @deno-types="./node_modules/@types/serialport/index.d.ts"
import * as SerialPort from "https://dev.jspm.io/serialport";

the error is:
error: relative import path "stream" not prefixed with / or ./ or ../ Imported from "file:///home/user/code/deno-serial-test/node_modules/@types/serialport/index.d.ts"

meaning it complains because the npm downloaded library dependencies aren't relative paths. this would mean i would need to change/patch all library modules.


the code i'm trying to run is:

// import * as SerialPort from "https://dev.jspm.io/npm:serialport@9.0.0/lib/index.dew.js";

// @deno-types="./node_modules/@types/serialport/index.d.ts"
import * as SerialPort from "https://dev.jspm.io/serialport";

async function readAvailablePorts() {
    const ports = await SerialPort.list();
    console.log('Available SerialPorts: ', ports);
}

readAvailablePorts();


any ideas how to solve would be appreciated.


Solution

  • You can't use NPM serialport package on Deno, it relies heavily on Node.js APIs, including stream which hasn't been polyfilled by Node Compatibility Library

    according to an article packages from pika.dev can be used. but from pika i get:

    You can use packages that uses plain JavaScript, not packages that rely on Node.js APIs.


    You'll have to build your own package or wait until one is written if you want to get readings from Arduino in Deno


    You can read more regarding using NPM modules on Deno on: How to use npm module in DENO?