thisdavej / ds18b20-raspi

Get temperature readings from a DS18B20 1-Wire sensor connected to a Raspberry Pi
MIT License
17 stars 4 forks source link

given bin in project.json (for CLI usage) is a JavaScript file not a runnable #11

Open niklas-englert opened 3 years ago

niklas-englert commented 3 years ago

Issue:

Calling ds18b20 does not work on linux machines. Nothing happens on an $ ds18b20 or $ ds18b20 -v call.

Steps to reproduce:

  1. Use any debian/ubuntu/raspbian/... machine.
  2. Run npm install -g ds18b20-raspi to install this module globally.
  3. Run ds18b20 or ds18b20 -v. See that nothing happens.

My bug tracking explained:

  1. Checked correct installing of module via $ ls /usr/lib/node_modules/ds18b20-raspi/. Was correct.
  2. Checked correct working of 1w via $ ls /sys/bus/w1/devices/. Was correct.
  3. Tried reproducing this on another machine. Same error.
  4. Traced back shell command ds18b20 via $ type ds18b20. Command registered under /usr/bin/ds18b20.
  5. /usr/bin/ds18b20 is a symbol linking to /usr/lib/node_modules/ds18b20-raspi/cli.js while JS files aren't executable in shells like that. More on that later.
  6. Read this repository and the npm doc. Traced back this issue to the package.js of this repository.
  7. Found short term fix: Using $ node /usr/lib/node_modules/ds18b20-raspi/cli.js [deviceId] [options] instead of $ ds18b20 [deviceId] [options] Found long term fix: Reporting this issue and hoping for a fix by the maintainer. – Providing a long term fix in the opened issue.

Found cause explained:

To quote the current package.json (line 12-14):

"bin": {
  "ds18b20": "cli.js"
},

This does not actually work on an global installation using npm install -g ds18b20-raspi. The provided script for bin has to be a binary or at least an executable, not a JavaScript file.

For some reasons this page of the official npm doc may uses a JS file for an example ({ "bin" : { "myapp" : "./cli.js" } }), but this apparently doesn't work on no machine on default whatsoever. npm will just link the given file (bin object value) in the /usr/bin/ with the given name (bin object key) and will make this symbol executable. Nothing more.

This in particular does not even work if you force it to run with node because of relative paths in cli.js remain unresolved under the symbolic location:

shell
< node $(type ds18b20)
> internal/modules/cjs/loader.js:883
>  throw err;
>  ^
>
> Error: Cannot find module '/root/ds18b20'
>    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
>    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
>    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
>    at internal/main/run_main_module.js:17:47 {
>  code: 'MODULE_NOT_FOUND',
>  requireStack: []
>}

Proposed fix:

  1. Adding a new file cli.sh to this repository which redirects to the node file:
    
    #!/bin/sh
    # call node script and pass to it all given arguments
    node /usr/lib/node_modules/ds18b20-raspi/cli.js "$@"

2. Changing the path in above quoted lines in the `package.json` to:
```js
"bin": {
 "ds18b20": "cli.sh"
},
  1. (optional) To follow conventions cli.sh and cli.js should be moved in a separat folder called bin/. Paths in 1. and 2. have to be adapted to this change of cause.

That should do it. Anyway, I really like using this module. Keep up the good work! :+1: :heart:

Appreciatively
Niklas Englert

(English is not my mother tongue; please excuse any errors on my part.)

thisdavej commented 3 years ago

@niklas-englert thanks for the detailed description of the issue you experienced! I apologize for my delayed response. I just installed the latest version of Raspberry PI OS (2020-08-20) and installed the latest version of Node (v15.2.1) following the instructions in my Beginner's Guide to Installing Node.js on a Raspberry Pi.

I then invoked the following command to install the ds18b20-raspi package (running as the pi user):

sudo npm install -g ds18b20-raspi

I was then able to run ds18b20 -v and the cli version of the package successfully launched.

I'm not sure what is different about your setup. Perhaps you are using a very old version of Node on the Raspberry Pi?

lynniemagoo commented 3 years ago

I have to ask when you installed nodejs, you also did so using sudo? Based on my quick read of the guide, that would be the case. You are also running on a Pi3B or Pi4B. I plan on using this for a Pi Zero where I manually install nodejs without using sudo apt-get.

sudo npm install -g ds18b20-raspi installs with elevated permissions as you are granting super user permission with sudo. Therein lies the difference.

npm install -g ds18b20-raspi installs as the current user (likely 'pi' on Raspbian) and not 'root' or 'su'.

niklas-englert commented 3 years ago

@lynniemagoo If a answer of mine is relevant: I technically do not use sudo. Since I am already always logged in with the superuser via SSH, that would be redundant.

Here's the snippet of my private setup script I use (if you're interested):

# get dependencies
apt install curl build-essential # ...
# run Node.js setup
# BEFORE THAT!: Check for a newer instructions https://github.com/nodesource/distributions#debinstall
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
apt-get install -y nodejs
# test success
node -v
nodejs -v
npm -v
lynniemagoo commented 3 years ago

I normally login as user ‘pi’

Sent from my iPhone

On Feb 8, 2021, at 7:45 PM, Niklas E. notifications@github.com wrote:

 @lynniemagoo If an answer is still relevant: I technically do not use sudo. Since I am already always logged in with the superuser via SSH, that would be redundant.

Here's the snippet of my private setup script I use (if you're interested):

get dependencies

apt install curl build-essential # ...

run Node.js setup

BEFORE THAT!: Check for a newer instructions https://github.com/nodesource/distributions#debinstall

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - apt-get install -y nodejs

test success

node -v nodejs -v npm -v — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.