Maker of open-source software and hardware.

Using MTP on macOS with Node.js: Part 2

Feature image

Following up from yesterday, I started using libmtp to see if I can connect to an Android phone and read a file from it. Once the library is successfully compiled, you can run some of the example programs in the examples/ folder. To list all the files on your device, run ./mtp-files. Then, to retrieve a file, you can run

./mtp-connect --getfile <FILE_NUMBER> <DESTINATION>

It seems to work just fine, so the next step is to wrap it as a Node.js library. There's a great article by Mathias Buus on the past and present of handling native (C/C++) modules in Node.js. Basically, N-API is the way to go. It's already stable in Node v10, and works in Node v8 as well.

I followed this tutorial to build a basic native module with N-API, and it works great. The best part was building it and running it in Node v10, and then running the same code in Node v8 without any issues. Nice!

#nodemtp