npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

njs-modbus

v1.4.0

Published

A pure JavaScript implemetation of Modbus for NodeJS.

Readme

njs-modbus

A pure JavaScript implementation of MODBUS for NodeJS.

npm download npm latest package npm bundle size

Introduction

njs-modbus is designed as a layered architecture, including the physical layer and the application layer:

  • Physical layer implements Serial Port, TCP/IP and UDP/IP.
  • Application layer implements RTU, ASCII and TCP.

njs-modbus provide both client and server.

Features

  • Full modbus standard protocol implementation
  • Support for custom function codes
  • Support broadcasting
  • Very lightweight project
  • Full typescript

Supported function codes

| Code | | | ----- | ----------------------------- | | 01 | Read Coils | | 02 | Read Discrete Inputs | | 03 | Read Holding Registers | | 04 | Read Input Register | | 05 | Write Single Coil | | 06 | Write Single Register | | 15 | Write Multiple Coils | | 16 | Write Multiple Registers | | 17 | Report Server ID | | 22 | Mask Write Register | | 23 | Read/Write Multiple Registers | | 43/14 | Read device Identification |

Supported protocols

  • Modbus RTU
  • Modbus ASCII
  • Modbus TCP/IP
  • Modbus UDP/IP
  • Modbus RTU/ASCII Over TCP/IP
  • Modbus RTU/ASCII Over UDP/IP

Installation

npm install njs-modbus

Examples

Modbus RTU Master

import type { ModbusSlaveModel } from 'njs-modbus';
import { SerialPhysicalLayer, RtuApplicationLayer, ModbusMaster } from 'njs-modbus';

const physicalLayer = new SerialPhysicalLayer({ path: 'COM1', baudRate: 9600, dataBits: 8, parity: 'none', stopBits: 1 });
const applicationLayer = new RtuApplicationLayer(physicalLayer);

const modbusMaster = new ModbusMaster(applicationLayer, physicalLayer);

modbusMaster
  .open()
  .then(() => {
    console.log('opened');
    modbusMaster.readHoldingRegisters(1, 0, 10).then((res) => {
      console.log(res);
    });
  })
  .catch((error) => {
    console.log(error);
  });

Modbus RTU Slave

import { SerialPhysicalLayer, RtuApplicationLayer, ModbusSlave } from 'njs-modbus';

const MB_SERVER = {
  discreteInputs: new Map<number, boolean>(),
  coils: new Map<number, boolean>(),
  inputRegisters: new Map<number, number>(),
  holdingRegisters: new Map<number, number>(),
};

const physicalLayer = new SerialPhysicalLayer({ path: 'COM1', baudRate: 9600, dataBits: 8, parity: 'none', stopBits: 1 });
const applicationLayer = new RtuApplicationLayer(physicalLayer);
const slave1: ModbusSlaveModel = {
  readDiscreteInputs: (address, length) => {
    return Array.from({ length }).map((_, i) => {
      const discreteInput = MB_SERVER.discreteInputs.get(address + i);
      if (typeof discreteInput === 'undefined') {
        return false;
      }
      return discreteInput;
    });
  },

  readCoils: (address, length) => {
    return Array.from({ length }).map((_, i) => {
      const coil = MB_SERVER.coils.get(address + i);
      if (typeof coil === 'undefined') {
        return false;
      }
      return coil;
    });
  },
  writeSingleCoil: (address, value) => {
    MB_SERVER.coils.set(address, value);
  },

  readInputRegisters: (address, length) => {
    return Array.from({ length }).map((_, i) => {
      const inputRegister = MB_SERVER.inputRegisters.get(address + i);
      if (typeof inputRegister === 'undefined') {
        return 0;
      }
      return inputRegister;
    });
  },

  readHoldingRegisters: (address, length) => {
    return Array.from({ length }).map((_, i) => {
      const holdingRegister = MB_SERVER.holdingRegisters.get(address + i);
      if (typeof holdingRegister === 'undefined') {
        return 0;
      }
      return holdingRegister;
    });
  },
  writeSingleRegister: (address, value) => {
    MB_SERVER.holdingRegisters.set(address, value);
  },

  reportServerId: () => ({ additionalData: [1, 2, 3] }),

  readDeviceIdentification: () => ({
    0x00: 'Basic:VendorName',
    0x01: 'Basic:ProductCode',
    0x02: 'Basic:MajorMinorRevision',
    0x03: 'Regular:VendorUrl',
    0x04: 'Regular:ProductName',
    0x05: 'Regular:ModelName',
    0x06: 'Regular:UserApplicationName',
    0x80: 'Extended:Extended',
    0xff: 'Extended:Extended',
  }),
};

const modbusSlave = new ModbusSlave(applicationLayer, physicalLayer);
modbusSlave.add(slave1);

modbusSlave
  .open()
  .then(() => {
    console.log('opened');
  })
  .catch((error) => {
    console.log(error);
  });

For more advanced examples, check out examples included in the repository. If you have created any utilities that meet a specific need, feel free to submit them so others can benefit.

Contributing

Please read our contributing guide first.

License

gitHub license