4

TypeScript code

import ModbusRTU from 'modbus-serial';
var modbus = new ModbusRTU();
modbus.connectTCP("192.168.1.5", { port: 502 });
modbus.setID(1);

setInterval(function() {
    modbus.writeRegister(2048, 2);
}, 100);

transpiles to

"use strict";
exports.__esModule = true;
var modbus_serial_1 = require("modbus-serial");
var modbus = new modbus_serial_1["default"](); //  <-- HERE
modbus.connectTCP("192.168.1.5", { port: 502 });
modbus.setID(1);
setInterval(function () {
    modbus.writeRegister(2048, 2);
}, 100);

However, the code only works without the ["default"] in class instantiation - like so

"use strict";
exports.__esModule = true;
var modbus_serial_1 = require("modbus-serial");
var modbus = new modbus_serial_1(); //  <-- HERE
modbus.connectTCP("192.168.1.5", { port: 502 });
modbus.setID(1);
setInterval(function () {
    modbus.writeRegister(2048, 2);
}, 500);

How to tell the transpiler to nod add it to the code?

peter.babic
  • 3,214
  • 3
  • 18
  • 31

1 Answers1

2

In case anyone else is running into this problem. It seems like the exports of this library are a bit faulty. I opened an issue here and a pull request to fix it here.

Edit: My patch was applied to this library, hence this issue should not arise in the future :)

Coxer
  • 1,694
  • 2
  • 26
  • 44