Playing a local file on RPi with Node.js - javascript

I have a Raspberry Pi set up with a Node.js app that responds when it sees button push from an Amazon Dash Button. It was originally supposed to be a silent doorbell from https://github.com/initialstate/silent-doorbell, but I would like to just have it play a local sound file. Which I think should be easy enough, but my inexperience with coding leaves me just trying new stuff that I find all over the internet.
I can play the file from the terminal with the following and it plays just fine:
$ omxplayer example.mp3
But, no matter how I try to put it in the Node.js app and trigger when the Dash Button is pressed it won't work.
var dash_button = require('node-dash-button'),
dash = dash_button('XX:XX:XX:XX:XX:XX'), //REPLACE WITH YOUR ADDRESS
exec = require('child_process').exec;
Omx = require('node-omxplayer');
player = Omx('~/node_modules/node-dash-button/example.mp3');
let spawn = require('child_process').spawn;
dash.on('detected', function() {
console.log('Button pushed!');
player.play();
});
When run with my latest as above, I get this:
/home/pi/node_modules/node-dash-button/doorbell.js:7
let spawn = require('child_process').spawn;
^^^^^
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
After upgrading Node.js to the newest version as suggested by #Quentin using the Major Version Upgrade directions on this site http://thisdavej.com/upgrading-to-more-recent-versions-of-node-js-on-the-raspberry-pi/ I was able to get past this. Now I can't get past how to properly use omxplayer. When running the same code as above after the Node.js upgrade I now get this error after pressing the Amazon Dash button which then crashes the app:
pi#raspberrypi:~/node_modules/node-dash-button $ sudo node doorbell.js
Button pushed!
/home/pi/node_modules/node-omxplayer/index.js:103
throw new Error('Player is closed.');
^
Error: Player is closed.
at writeStdin (/home/pi/node_modules/node-omxplayer/index.js:103:10)
at EventEmitter.Omx.omxplayer.play (/home/pi/node_modules/node-omxplayer/index.js:133:27)
at Readable.<anonymous> (/home/pi/node_modules/node-dash-button/doorbell.js:13:12)
at emitOne (events.js:96:13)
at Readable.emit (events.js:188:7)
at PcapSession.<anonymous> (/home/pi/node_modules/node-dash-button/index.js:87:28)
at emitOne (events.js:96:13)
at PcapSession.emit (events.js:188:7)
at PcapSession.on_packet_ready (/home/pi/node_modules/node-dash-button/node_modules/pcap/pcap.js:99:10)
at packet_ready (/home/pi/node_modules/node-dash-button/node_modules/pcap/pcap.js:44:14)
I tried a few different things to try and get the player to spawn with no luck. The index.js file referenced mentions to use the player.running command but I still get the player is closed error when attempting to use this.

You are using a version of Node older than the 4.x series.
Consequently it sees let as an identifier rather than the keyword, so it doesn't expect it to be immediately followed by another identifier (spawn).
Upgrade your installation of Node to a current version.
Alternatively, use a different variable declaration, such as var.

Related

Beginning JavaScript question using the Eclipse IDE

I'm a beginner at JavaScript so go easy on me here. (This is not supposed to be Java. I know they're different)
So I'm trying to run my first script in the Eclipse IDE and the code is just:
document.write("<h1>Hello World<h1>");
But my console says:
<terminated> js Proj 2-ffirst.js [Node Application] Node.js Process
at Object.<anonymous> (D:\Javascript\js Proj 2\ffirst.js:1:1)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47
I suspect my issue is in how I set up the software but I haven't learned how to use this yet really so I'm open to any suggestions you have. Thanks!
you are running a node app - as node is on the server it doesn't know what the document and window are (they are browser api's only accessible on the client side)
Because they don't know what the document Object is you get an error.
Try either console.log to print a response in the terminal from your node server or move to something like VS code to write a plain .js file that can be launched through the browser.

Grave (`) is marked as illegal token

I've been trying to transfer a node program I created locally to an EC2 server via Cloud9, and I have already manually replaced all of the (req,res) => {} statements because it didn't accept that. However, now it's giving me
console.log(`foo ${bar}`)
^
SyntaxError: Unexpected token ILLEGAL
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:945:3
It works fine on my local server and it runs fine here too, even without nodejs.
var bar = "bar"
console.log(`foo ${bar}`)
The version of node is v13.9.0 and it was installed via nvp.
Anyone know why this is happening? I really don't want to go through and convert all the strings with `
Thanks for the help, I figured out that cloud9 automatically installs nodejs, just not the right version. It installs 10.19.0, while the current latest version is 13.10.0. I installed the latest version, but the default was still 10.19.0. For anyone else having this problem, do
nvm ls
to list your installed node versions, then use
nvm alias default 13.10.0 (replace with whatever version you want)

Syntax error at new Script (vm.js:51:7) whilst running code for a discord bot in javascript

I get an error whilst running my node index.js
here is my code
const botconfig = require("./botconfig.json");
const Discord = require("discord.js");
const bot = new Discord.Client({disableEveryone: true});
bot.on("ready", async () => {
console.log(`${bot.user.username} is online!`);
}};
bot.login(botconfig.token);
here is my error message
SyntaxError: missing ) after argument list
at new Script (vm.js:51:7)
at createScript (vm.js:138:10)
at Object.runInThisContext (vm.js:199:10)
at Module._compile (module.js:624:28)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:190:16)
hope you can help!
If you see an error at new Script (vm.js:51:7), it means there's an error in a custom script that you passed to vm.js, the Node module that communicates with the V8 Virtual Machine.
new Script in vm.js is simply evaluating your code.
So you need to work out what the fault is in the code you passed to the V8 virtual machine. If you run the file directly (e.g. node some/path/some_file.js) you should get a pointer to the actual fault that looks like this:
YourPC:your-directory you$ node some/path/some_file.js
/some/system/path/your-directory./some/path/some_file.js:123
}};
^
SyntaxError: missing ) after argument list
at new Script (vm.js:51:7)
at createScript (vm.js:138:10)
at Object.runInThisContext (vm.js:199:10)
The part above the error message with the ^ caret shows you the faulty point in your own code.
In your case, it's pretty easy to spot: you have a }}; that should be a });.
If you have code that seems 100% fine but encounters this error, like #maevanapcontact's failing arrow functions, maybe you're using an old version of Node with an old version of V8 that didn't support that ECMAScript feature. Arrow functions didn't have complete support until Node version 6.
I had the same error as you and I have fixed it by using function(){}; instead of () =>.
I don't really know why it doesn't work with arrow functions, but it did the job for me like that.
I found it helpful to add a break-point in vm.js where the error is thrown. (click on the filename link in the stack trace).
Reload the page and then inspect the local variables.
The filename variable will give the the full name of the js file that caused the error.
Unfortunately this isn't enough to narrow down the exact line, so I ended up deleting parts of the file until it would compile. From there I was able to narrow what was causing the error. In my case, my IDE linter wasn't giving me any hints either.
The following helped me.
Delete all node_modules
rm -rf node_modules/
and then install
npm install

I get a SyntaxError in Node.JS, but I can't figure out the origin of that error from the log messages

I am running a Node app on Azure. It's a Web API and when I try to call it, I see this message in the Node console:
Application has thrown an uncaught exception and is terminated:
SyntaxError: Unexpected token (
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:414:25)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (D:\home\site\wwwroot\app.js:17:23)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
I ran my whole code through eslint and didn't find any syntax errors. It also runs fine on my local machine, but doesn't run on Azure.
The message above points to line 17 and column 23 in app.js, which is a require:
let warehouseExport = require('./routes/warehouse-export-api');
I ran eslint and jshint specifically on the file referenced above, and there are no syntax errors.
What are some other ways I can get the exact location of the syntax error, and why could it be that the code fails on Azure but runs on my local machine?
What I tried:
Wrap in try-catch and grab the line number - it shows up undefined:
try {
var warehouseExport = require('./routes/warehouse-export-api');
} catch (error) {
console.log(error + ', line ' + error.lineNumber);
}
You should check version of nodejs you have installed on Azure. Maybe it's <= 4 and it doesn't support let keyword. Just a wild guess.
The cause of the error was that the Node version somehow got reset to 4.2.4.
When I go to Azure Portal and run node --version, I get version 8.1.4.
However, if I console.log the Node version inside my script, it is somehow still 4.2.4.
I will investigate why that happened and how to fix it, but for now it's clear that the bug is due to async function not being recognized by the old Node.
Update: the package.json file where the node version was specified got overwritten somehow, which is what caused the issue.

PM2 Managing PHP Script - Cluster mode

Currently i'm successfully managing some PHP deamons (single instance) with PM2, and, so far so good!
With PM2 and while managing Node.js/IO.js apps, i can launch them in cluster mode on PM2 with no stress! The same does not happen with the mentioned php deamons!
So, first of all, is it even possible to manage/launch clustered instances of a PHP script using PM2 (or is that PM2 feature only usable on Node.js/IO.js scripts)?
If so (possible to do), what special "tuneups" are required to cluster the PHP scripts with PM2!?
While trying to launch some pm2 start xxx.php -i 2 --name XXX, i get the following error # the error log path:
SyntaxError: Unexpected token <
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function._load (/usr/local/lib/node_modules/pm2/node_modules/pmx/lib/transaction.js:62:21)
at /usr/local/lib/node_modules/pm2/lib/ProcessContainer.js:200:23
at /usr/local/lib/node_modules/pm2/node_modules/async/lib/async.js:52:16
at /usr/local/lib/node_modules/pm2/node_modules/async/lib/async.js:1209:30
at WriteStream.<anonymous> (/usr/local/lib/node_modules/pm2/lib/Utility.js:126:13)
at WriteStream.EventEmitter.emit (events.js:95:17)
... that makes me believe that it can't even parse the given PHP file!
Thanks in advance for any help on this!
Short answer: you can't use cluster mode with php.
The pm2 cluster mode only works with javascript. Internally it's using the nodejs cluster module and requiring your script (code line).
This also explains your error, pm2 tries to execute the php code with javascript (hence the SyntaxError).
When using fork mode, it's using your interpreter (here php) to launch the script. This is why you can also run python or ruby on top of nodejs!

Categories