alwint3r / sht31-node

Module for reading SH31 sensor through i2c on Raspberry Pi
MIT License
1 stars 1 forks source link

'usage' code doesn't work without modification #1

Open matteliot opened 7 years ago

matteliot commented 7 years ago

Grabbed the code, made a file that was a direct copy from the README Usage section.

It can't load "sht31".

Is it intended that the usage code ought to be usable without any modifications? If not, you might want to call that out.

alwint3r commented 7 years ago

It should work without any modification at best case. You might only need to change the I2C address or the I2C bus number that given to the constructor.

Could you elaborate me how you use this library?

matteliot commented 7 years ago

If you take the example code from readme, put it into a file and try to run it, it errors out because SHT isn't defined.

I'm using node 7.9, but I don't think it matters in this case.

"use strict";

const test= require("./index");

//const SHT31 = require(`sht31`); **//ORIGINAL CODE with error**
const SHT31 = require(`./index`).SHT31; **//this one works**
const sht31 = new SHT31(0x44, 1); // address: 0x44, device = /dev/i2c-1

sht31
  .init()
  .then(() => sht31.readSensorData())
  .then((data) => {
    console.log(data);
  })
  .catch((err) => {
    // ...
    // Handle errors
console.log("Error: " + err);
  });

here's the error you'd get with the original code:

/projects/sht31-node/src # node origRun.js 
module.js:472
    throw err;
    ^

Error: Cannot find module 'sht31'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/projects/sht31-node/src/origRun.js:1:77)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
alwint3r commented 7 years ago

Alright, I think the way you use this library is kinda wrong in some way. The code in the "usage" section of README is assuming that you have installed the module as a depencency. I also said in another issue of yours, that this index.js file should not be run directly.

Your stacktrace said that node can not find this module in your system. Error: Cannot find module 'sht31'

So, I think this is the right & "common" way:

In the end, your code is working although I think it is not the preferred way of using a node module.