Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
467 stars 174 forks source link

Got tsc TS2307 error - could not find a declaration file for module 'serialport' #280

Closed vn7n24fzkq closed 3 years ago

vn7n24fzkq commented 3 years ago

Hi, thanks for this awesome lib. I got an error - " could not find a declaration file for module serialport' " when I use tsc to build a typescript project.

I am using jsmodbus:4.0.5 and I also check the package-lock.json, the require block only contains crc : 3.4.0 and debug : ^3.1.0, I think serialport should be put in dependencies too, not just in devDependencies.🤔

alexbuczynsky commented 3 years ago

That's a good point. It seems the type declaration missing is a problem when tsc is trying to parse the type declarations.

I believe the original design choice was to make the dependency on serialport optional if you are not using modbus RTU (correct me if I am wrong @stefanpoeter). Given the dynamic loading of vanilla javascript, this is not a problem since the module is dynamically loaded in if the RTU files are referenced at runtime. However with typescript, the entire projects types are checked and if the serial port dependency is missing the type info, the transpiler will break. Given this, if we make serialport a required dependency this would fix the problem.

alexbuczynsky commented 3 years ago

@vn7n24fzkq if you install serialport in your project without the type declarations does the tsc build pass or do you need to install the dev dependency on @types/serialport as well?

vn7n24fzkq commented 3 years ago

@alexbuczynsky No, the tsc build would not pass if I don't install @types/serialport in dev dependencies.

stefanpoeter commented 3 years ago

I believe the original design choice was to make the dependency on serialport optional if you are not using modbus RTU (correct me if I am wrong @stefanpoeter).

That is absolutely correct. But you could build an interface for the client handling the events for both the net module and the serialport module. You can then make wrapper for both modules. Shouldn't be that hard. Correct me if I am wrong @alexbuczynsky

vn7n24fzkq commented 3 years ago

@stefanpoeter Thanks for the reply. Yes, I can make a wrapper for both modules, but I am not using Modbus RTU.

My temporary solve way is installing serialport and @types/serialport to avoid tsc build errors. It is a little weird that I need install a module I don't need.

Like @alexbuczynsky said, vanilla javascript uses dynamic loading, so we can choose which module is unnecessary in the vanilla javascript case.

If we don't want to add net and serialport modules into the required dependency then maybe we can add a doc to remind that typescript needs to install both modules for type declaration when tsc build? ( Not sure would other js compilers receive the same error )

alexbuczynsky commented 3 years ago

I like the interface approach. They should honor the abstraction of the specific function / properties we need from these two libraries (net and serialport).

I will take a look at how to design the interface to work with tsc.

alexbuczynsky commented 3 years ago

Upon further investigation, it seems like in Typescript 3.8 they introduced importing just the type information from a module. Since this library only needs to access the type information and not the classes themselves, this might be a nice approach.

vn7n24fzkq commented 3 years ago

@alexbuczynsky Thanks a lot! That would be a good approach. I will try it tomorrow.

vn7n24fzkq commented 3 years ago

I can successfully build by installing @type/serial and don't need to install serialport now.

stefanpoeter commented 3 years ago

I think that resolves this issue. Good work :-)

yorch commented 3 years ago

@stefanpoeter just faced this issue and found this discussion, thanks for all the good work!

Just my 2 cents, I think the correct approach given that jsmodbus package type definition includes serialport ts definition, would be to include @types/serialport as dependency. I don't think TS / Node communities has solved the problem in any other more efficient way unfortunately, but it's what other libraries / packages have to do.

https://stackoverflow.com/a/46011417/443600

tukusejssirs commented 2 years ago

My two cents: In TS in Nest.js (but Nest.js seems irrelevant), after installing jsmodbus, and starting the app, it would fail to start because it does not find @types/serialbus. After installing that package manually, it works as expected.

I think that @types/serialbus should be a dependency of jsmodbus, not of the program that uses jsmodbus.