How do I get 'onoff' working on a Raspberry Pi 4B? - javascript

I am trying to make a 'Coocking-aid'. I am using a Raspberry Pi 4B with the official 7" touchscreen and an active piezo buzzer. The 'program' should run in Chromium (the RPi's webbrowser) and I write it in HTML, CSS and Javascript. I can do some tricks in Javascript but lack a lot of knowledge there. I try to sound the buzzer with Javascript, but so far without success.
The buzzer is connected between GPIO-pin 17 and Ground (connector-pins 11 and 9). When GPIO 17 is made high (+3V) the buzzer will sound.
I have tested this 'circuit' with a small Python program, which successfully turned the buzzer on:
1. from gpiozero import Buzzer
2. buzzer = Buzzer(17)
3. buzzer.on()
To do the same with Javascript I learned that I needed to install Node.js and its module 'onoff'.
From this web-post 1 I learned how to install node.js, by typing the following commands in the terminal:
curl -sL https://deb.nodesource.com/setup_15.x | sudo -E bash -
sudo apt install -y nodejs
I checked the installation by typing: "node -v". The response was: "v15.12.0"
I also checked the version of Node's package manager by typing: "npm -v". The response was: "7.7.5"
From this web-post 2 I learned how to install "onoff" by typing the following command in the terminal:
npm install onoff
I checked the installation by typing: "node -v". The response was "v15.12.0"
In the RPi directory "/home/pi/node_modules/onoff/examples" is a file called: "blink-led.js"
which I used as an example to create (in the same directory) a file called: "test.js" with the following code:
'use strict';
const Gpio = require('../onoff').Gpio; // Gpio class
const buzzer = new Gpio(17, 'out'); // Export GPIO17 as an output
buzzer.writeSync(17); // Make GPIO-pin 17 high
In the terminaI I moved to the same directory by typing:
cd /home/pi/node_modules/onoff/examples
and then executed "test.js" file by typing:
node test.js
Unfortunately the buzzer was not activated and a comprehensive error message was shown (see under).
I get the impression that the numbers are program line numbers and I had a look here and there, but I can't figure out what is wrong.
Any help would be much appreciated!.
The error message was:
/home/pi/node_modules/bindings/bindings.js:121
throw e;
^
Error: libnode.so.64: cannot open shared object file: No such file or directory
at Object.Module._extensions..node (node:internal/modules/cjs/loader:1151:18)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:996:19)
at require (node:internal/modules/cjs/helpers:92:18)
at bindings (/home/pi/node_modules/bindings/bindings.js:112:48)
at /home/pi/node_modules/epoll/epoll.js:7:31
at Object.<anonymous> (/home/pi/node_modules/epoll/epoll.js:15:3)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10) {
code: 'ERR_DLOPEN_FAILED'
}

Related

Streamr node install on putty (issues)

The guide I am using https://www.youtube.com/watch?v=9cdsQWRDNM4 time marker (7:27) is where it goes to all wrong.
This is where I am having issue with my Streamr node install. Its a very basic install. The issue I am having is I'm very new to putty and this section isn't the same as the install is supose to be -- /root/.streamr/config/default.json but when I change it it becomes another error. The following sentence is what I am generating which becomes an error also which prevents myself from starting my node up (on my vps) Select a path to store the generated config in /home/streamr/.streamr/config/default.json
Either way I am getting an error.
**This is a screen shot of what I am dealing with !!! Any help with putty and setting up VPS would be great. **
can provide actual screenshot if needed!!
: Welcome to the Streamr Network
: Your node's generated name is Leopard Wrist Pond.
: View your node in the Network Explorer:
:
: You can start the broker now with
: streamr-broker /home/streamr/.streamr/config/default.json
root#ubuntu-s-1vcpu-1gb-tor1-01:~# docker run -it -p 7170:7170 -p 7171:7171 -p 1883:1883 -v $(cd ~/.streamr Docker; pwd):/root/.streamr streamr/broker-node:latest streamr/broker-node:lates **Error: Config file not found in the default location. You can run "streamr-broker-init" to generate a confi g file interactively, or specify the config file as argument: "streamr-broker path-to-config/file.json"
at readConfigAndMigrateIfNeeded (/home/streamr/network/packages/broker/dist/src/config/migration.js:155 ** :19)
at Command. (/home/streamr/network/packages/broker/dist/bin/broker.js:20:69)
at Command.listener [as _actionHandler] (/home/streamr/network/node_modules/commander/lib/command.js:48 8:17)
at /home/streamr/network/node_modules/commander/lib/command.js:1227:65
at Command._chainOrCall (/home/streamr/network/node_modules/commander/lib/command.js:1144:12)
at Command._parseCommand (/home/streamr/network/node_modules/commander/lib/command.js:1227:27)
at Command.parse (/home/streamr/network/node_modules/commander/lib/command.js:897:10)
at Object. (/home/streamr/network/packages/broker/dist/bin/broker.js:41:6)
at Module._compile (node:internal/modules/cjs/loader:1218:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
npm notice
npm notice New major version of npm available! 8.19.3 -> 9.4.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.4.2
npm notice Run npm install -g npm#9.4.2 to update!
npm notice
Ive tried both ways for the file name but both end in errors. Im nt sure why its not generating a /root/.streamr/config/default.json. Its giving me this strange file name that doesn't work either streamr-broker /home/streamr/.streamr/config/default.json. This one will let me finish the set up of the docker but it will not let me start the node and generates an error as seen in the code!

Program 'npx.cmd' failed to run

I'm trying to create a JS app using the VSCode Terminal. To do so, I'm typing in the following: npx #knowledgehut/create-js-app my-blog but I get the following error as a result:
ResourceUnavailable: Program 'npx.cmd' failed to run: An error occurred trying to start process 'C:\Program Files\nodejs\npx.cmd' with working directory 'C:\Users\belen'. The system cannot find the file specified.At line:1 char:1
npx #knowledgehut/create-js-app my-blog`
I have already unistalled an reinstalled VSCode and same with Node.js. I have of course restarted my computer because sometimes that does the trick but nothing worked.
VS Code version: Code 1.69.2 (3b889b0, 2022-07-18T16:12:52.460Z)
OS version: Windows_NT x64 10.0.22000
Restricted Mode: No

Next.js doesn't load jsx

I have been searching about Next.js, the javascript framework of the moment, and I want to begin to practice. The problem is: it doesn't run in my computer by any means.
The natural form on creating a next project is with the command line:
npx create-next-app
or
yarn create next-app
The result is a template project with a file structure and a few scripts to run the server. The most basic script runs the server in development mode:
npm run dev
or
yarn dev
The result in terminal is the warning:
$ yarn dev
yarn run v1.22.5
$ next dev
(node:5348) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution
of the package at C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\node_modules\postcss\package.json.
Update this package.json to use a subpath pattern like "./*".
(Use `node --trace-deprecation ...` to show where the warning was created)
ready - started server on http://localhost:3000
error - C:/Users/maths/Documents/codigos/node/my-app/pages/_app.js 4:9
Module parse failed: Unexpected token (4:9)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. S
ee https://webpack.js.org/concepts#loaders
|
| function MyApp({ Component, pageProps }) {
> return <Component {...pageProps} />
| }
|
And when I enter the website in localhost:3000, the page on browser shows:
Internal Server Error
After making the http requisition from the browser, the terminal shows the following message:
event - build page: /next/dist/pages/_error
wait - compiling...
error - C:/Users/maths/Documents/codigos/node/my-app/pages/_app.js 4:9
Module parse failed: Unexpected token (4:9)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. S
ee https://webpack.js.org/concepts#loaders
|
| function MyApp({ Component, pageProps }) {
> return <Component {...pageProps} />
| }
|
Error: Cannot find module 'C:\Users\maths\Meus Documentos\codigos\node\my-app\.next\server\pages-manifest.json'
Require stack:
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\require.js
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\load-components.js
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\api-utils.js
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\next-server.js
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\server\next.js
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\server\lib\start-server.js
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\cli\next-dev.js
- C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\bin\next
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:925:15)
at Function.Module._load (node:internal/modules/cjs/loader:769:27)
at Module.require (node:internal/modules/cjs/loader:997:19)
at require (node:internal/modules/cjs/helpers:92:18)
at getPagePath (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\require.js:1:
657)
at requirePage (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\require.js:1:
1062)
at loadComponents (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\load-compo
nents.js:1:807)
at DevServer.findPageComponents (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\ser
ver\next-server.js:74:296)
at DevServer.renderErrorToHTML (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\serv
er\next-server.js:126:120)
at DevServer.renderErrorToHTML (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\server\next-dev-
server.js:34:974)
at processTicksAndRejections (node:internal/process/task_queues:93:5)
at async DevServer.render (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\ne
xt-server.js:72:236)
at async Object.fn (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\next-serv
er.js:56:618)
at async Router.execute (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\rout
er.js:23:67)
at async DevServer.run (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\server\next-
server.js:66:1042)
at async DevServer.handleRequest (C:\Users\maths\Documents\codigos\node\my-app\node_modules\next\dist\next-server\se
rver\next-server.js:34:1081) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\next-server\\server\\require.js',
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\next-server\\server\\load-components.
js',
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\next-server\\server\\api-utils.js',
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\next-server\\server\\next-server.js',
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\server\\next.js',
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\server\\lib\\start-server.js',
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\cli\\next-dev.js',
'C:\\Users\\maths\\Documents\\codigos\\node\\my-app\\node_modules\\next\\dist\\bin\\next'
]
}
I have tried several things to fix it, create the next project without the script from npx and yarn (by manually installing the dependencies), update versions of node and npm, etc. It seems that the program can't load the jsx, the first error points to the moment when the first jsx component appear in the code. The message says that webpack isn't configured with a valid loader to this code.
Environment:
Win 10
npm v7.3.0
node v15.5.0
next v10.0.4
react v17.0.1
react-dom v17.0.1
Downgrade your version of Nodejs to 14 or 12 Node version 15 will break your environment. If you want to still want to use 15 get node version switcher and switch the version based on the projects your working on.

(Tutorial) Script does not run due SyntaxError: Block-scoped declarations

As per Quill's suggestion, I am posting this again. The first time I posted "npm update node" fixed the problem.
I bought a tutorial from Packtpub.com called "Web Development with NodeJs and MongoDB" and I want to run the sample tutorial code. Here is an example:
#!/usr/bin/env node
console.log('Argument vector');
console.log(process.argv);
let args = process.argv.slice(2);
console.log('\nArguments after slicing');
console.log(args);
let name = args[0];
if (!name) {
throw Error('\nYou have not given your name, bye');
}
console.log('\nHello ' + name);
So I download and install Node-v5.6.0-x64.msi. Why do I get errors when I use the let command:
-*- mode: compilation; default-directory: "c:/Users/v-sihein/Documents/books/2016/javascript/Web Development with Node.JS and MongoDB/3413OS_Section 01_Code/1/1.3/" -*-
Compilation started at Sat Feb 13 16:25:01
node 03-hello-arguments.js siegfried
c:\Users\v-sihein\Documents\books\2016\javascript\Web Development with Node.JS and MongoDB\3413OS_Section 01_Code\1\1.3\03-hello-arguments.js:9
let args = process.argv.slice(2);
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:140:18)
at node.js:1001:3
Compilation exited abnormally with code 1 at Sat Feb 13 16:25:01
Now if I change the "let"s to "var"s then it works!
I had this problem previously (a couple of days ago) and "npm update node" fixed the problem (I think -- maybe I mistakenly ran the version with the "var" instead of the "let"). However, I have since reinstalled windows 10 and now "npm update node" does not fix the problem any more.
So what versions of javascript and ecmascript can I expect to be able to execute with node v5.6.0? I've been boogle searching and I cannot find a simple table of javascript features for the different versions of node and javascript. I would think knowing what versions of javascript/ecmascript is supported by a version node would be very basic/important information to every node programmer! Can someone point me to such a table?
Did I buy a bad tutorial? I think the author uses linux or MAC and I'm using windows. Should not node work the same way on mac/linux and windows?
Please tell me if this program works for you on your computer. Be sure to mention if you are running mac, unix, linux or windows (or something else?).
Thanks
Siegfried
You should just add 'use strict' to the beginning of the js part
#!/usr/bin/env node
'use strict'
(...)
This will then immediately work. It's weird that the tutorial doesn't mention that.

Get error with "line.slice()" on "karma init"

I was started to read "Developing an AngularJS Edge", and I wanted to set up the various frameworks in use.
The book uses nodejs and karma, along with several other frameworks.
I'm on Win7x32.
I just upgraded my nodejs to the latest, v0.10.18 .
I installed the Karma package with "npm install -g karma". This appeared to complete successfully.
I then ran "karma init", which did this:
% karma init
>
readline.js:507
this.line = this.line.slice(this.cursor);
^
TypeError: Cannot call method 'slice' of undefined
at Interface._deleteLineLeft (readline.js:507:25)
at suggestNextOption (C:\Users\David\AppData\Roaming\npm\node_modules\karma\lib\init.js:167:9)
at nextQuestion (C:\Users\David\AppData\Roaming\npm\node_modules\karma\lib\init.js:235:12)
at process (C:\Users\David\AppData\Roaming\npm\node_modules\karma\lib\init.js:250:10)
at Object.exports.init (C:\Users\David\AppData\Roaming\npm\node_modules\karma\lib\init.js:352:6)
at Object.<anonymous> (C:\Users\David\AppData\Roaming\npm\node_modules\karma\bin\karma:25:37)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
Note that this error is virtually identical to the stack trace reported by this person a month ago, but that report has received no response.
Probably you're using the Git Bash terminal, MinTTY, which doesn't have full support for TTY.
You have 4 options to fix the problem:
Use CMD terminal with the Windows shell (the default Windows console)
Use CMD terminal with the Bash shell (execute "C:\Program Files\Git\bin\bash.exe" --login -i in cmd terminal)
Use Powershell (an alternative terminal typically installed by default in Windows environments)
Use an alternative terminal (or develop a new one :stuck_out_tongue_winking_eye: )
If you just need execute isolated interactive commands like karma init, I recommend option 1 for it and then come back to Git Bash. However I would give a try to Powershell.
The reason for the error with running npm in Cygwin seems to be a known issue with npm. You can use the work around mentioned here,
https://stackoverflow.com/a/22436199/2390020
Faced the same issue.try this
node node_modules/karma/bin/karma init
I got the same error with MINGW64.
Runs properly on default command prompt.
One other likely relevant point that I failed to mention is that I was running these commands from a Cygwin rxvt window.
I just tried completely uninstalling nodejs and reinstalling it.
When I brought up the rxvt window again and ran "npm install -g karma", it failed with the following:
/c/Program Files/nodejs/npm: line 2: $'\r': command not found
/c/Program Files/nodejs/npm: line 4: $'\r': command not found
/c/Program Files/nodejs/npm: line 5: syntax error near unexpected token `$'in\r''
'c/Program Files/nodejs/npm: line 5: `case `uname` in
That led me to search for that error on the web, and I found this thread, which implies that it simply doesn't support Cygwin (unfortunate, but not a huge problem).
When I then brought up a plain, ugly, unfriendly "cmd" window, both the karma installation and "karma init" completed successfully (or at least it gave me the first question in the "init" process).
In my case, the problem was that I was using the git bash terminal in windows. When I ran the command in a cmd window it worked fine.
Run the command in github shell command window. Worked for me.
If you open up the karma file it's just a node script, so an alternative way of executing it would be:
node karma init
This worked for me in the MINGW64/git bash shell

Categories