Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
456 stars 169 forks source link

TypeError: Cannot read properties of undefined (reading 'client') #322

Closed alishefaee closed 1 year ago

alishefaee commented 1 year ago

When using typescript, I get this error:


16:04:12 error unhandledRejection: Cannot read properties of undefined (reading 'client')
TypeError: Cannot read properties of undefined (reading 'client')

this is my snippet code:

// typescript
import { Socket, SocketConnectOpts } from 'net'
import Modbus from "jsmodbus";

let modbusSocket = new Socket()
const client = new Modbus.client.TCP(modbusSocket)

package.json:

{
  "main": "index.ts",
  "homepage": "https://github.com/alishefaee/jti-energy-management#readme",
  "dependencies": {
    "@types/express": "^4.17.15",
    "express": "^4.18.2",
    "jsmodbus": "^4.0.6",
    "socket.io": "^4.6.1",
    "typescript": "^4.9.4",
  },
  "devDependencies": {
    "ts-node-dev": "^2.0.0"
  }
}

What I tried: If I use

const Modbus = require('jsmodbus')

instead of:

import Modbus from "jsmodbus";

problem is solved.
Also there is no problem with js version.

carlmasonemr commented 1 year ago

I found specifically importing client resolved this for me:-

import { client } from 'jsmodbus';