Beginning JavaScript question using the Eclipse IDE - javascript

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.

Related

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)

How to Hide Internal Stack Trace Error in Node.js By Default?

I only want to display the TypeError line below, but hide all the internal details (the following 7 lines all starting with at).
~/Desktop/Practice> node "c:\Users\User\Practice\PracJS.js"
c:\Users\User\Practice\PracJS.js:6
let b = duck.prototype.isPrototypeOf(Bird)
^
TypeError: Cannot read property 'isPrototypeOf' of undefined
at Object.<anonymous> (c:\Users\User\Practice\PracJS.js:6:24)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11
I know I can do this with Error.stackTraceLimit = 0 by including it in the file. But I want to do this by default.
Is there any way to point Node a config file (setting Error.stackTraceLimit = 0 inside) so that whenever I run node PracJS.js (inside VSCode) so that it automatically applies Error.stackTraceLimit = 0?
Any help is appreciated! Thank you!
You could use --stack-trace-limit flag while running the javascript file. so in your case
node --stack-trace-limit=0 "c:\Users\User\Practice\PracJS.js"
OR
export NODE_OPTIONS='--stack-trace-limit=0'
It will be also worth mentioning to use NODE_ENV for these kind of cases. Some of the good packages (Express etc) uses this variable to control these things. so basically set NODE_ENV=production and you could also use this to control stack trace in your code.

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.

Playing a local file on RPi with Node.js

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.

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