Nodejs "SyntaxError: Unexpected token ." - node.js

I'm trying to run a Bitcoin insight explorer (https://www.dgbwiki.com/index.php?title=Running_your_own_Insight_explorer). Using node v0.10.48 but I get this error (couldn't find the same problem over the internet):
digibyte#derecha-virtual-machine:~/insight$ /home/digibyte/.nvm/v0.10.48/bin/node ~/insight/node_modules/insight-bitcore-api/util/sync.js -D -v --rpc
/home/digibyte/insight/node_modules/insight-bitcore-api/node_modules/async/dist/async.js:52
function apply(fn, ...args) {
^
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 Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/digibyte/insight/node_modules/insight-bitcore-api/lib/HistoricSync.js:5:22)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
The offending line 52 is in this function:
function apply(fn, ...args) { // <- line 52
return (...callArgs) => fn(...args,...callArgs);
}
It looks ok to me I don't know why node gives an error.

Node.js 0.10.48 doesn't support the spread operator. The first Node.js version to support the spread operator was 5, but it's quite outdated and isn't maintained anymore. If you're already upgrading, I'd upgrade to one of the newer version still supported under LTS.

According to https://node.green/#ES2015-syntax-rest-parameters, node v0.10.48 does not support rest parameters (...args).
You should use a newer version of node (at least v6.4.0 as default support, or at least v4.9.1 with --harmony flag (node --harmony))

Apply takes an array as the second argument. Here, the spread operator (...) is laying out the elements, and thus you don't pass an array into the function, but basically comma separated arguments.
Try using .call instead of .apply, or pass args instead of ...args.

Related

How to solve this 'SyntaxError: Invalid regular expression' in pm2

I install node.js with pm2, all install fine but when i try to do pm2 monit give me this error.
$ pm2 monit
/home/node/.nvm/versions/node/v8.11.3/lib/node_modules/pm2/node_modules/fast-printf/dist/src/tokenize.js:4
const TokenRule = /(?:%(?<flag>([+0-]|-\+))?(?<width>\d+)?(?<position>\d+\$)?(?<precision>\.\d+)?(?<conversion>[%BCESb-iosux]))|(\\%)/g;
^
SyntaxError: Invalid regular expression: /(?:%(?<flag>([+0-]|-\+))?(?<width>\d+)?(?<position>\d+\$)?(?<precision>\.\d+)?(?<conversion>[%BCESb-iosux]))|(\\%)/: Invalid group
at Object.<anonymous> (/home/node/.nvm/versions/node/v8.11.3/lib/node_modules/pm2/node_modules/fast-printf/dist/src/tokenize.js:4:19)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/node/.nvm/versions/node/v8.11.3/lib/node_modules/pm2/node_modules/fast-printf/dist/src/createPrintf.js:5:20)
at Module._compile (module.js:652:30)
I tried servers nodejs version and always give me this error.
If you paste your regular expression into a regex sandbox, like this one:
https://regex101.com/r/GkwWH2/1
You'll see that it doesn't show any syntax errors.
That would make me suspect that you're running a back-level version of Node JS. And in fact, if you look at your stack trace, you see that you are running Node 8.11.3
at Object.<anonymous> (/home/node/.nvm/versions/node/v8.11.3/lib/node_modules/pm2/node_modules/fas
That regular expression is using features (such as named capture groups) that did not come into Node.js until Node v10.*.
I suggest you update to the latest LTS version of Node, version 14.*: from your stack trace it would appear that you're using NVM, so it should be a simple matter of
sudo nvm install lts/fermium
should do it.

Use of const in strict mode (Node.js and Mongoose)

I have seen this question asked many times around here, but none of the solutions seem to fit my case.
I am using Node.js to access a MongoDB database. I am using Mongoose library. All on Ubuntu 14.04.
When trying to run "nodejs myfile.js" I get this error:
const deprecationWarning = 'The `MONGOOSE_DRIVER_PATH` global property is '
^^^^^
SyntaxError: Use of const in strict mode.
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 Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/root/nodejs/node_modules/mongoose/index.js:9:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
The main solution I found is upgrading Node.js with this commands:
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
but the result after is the same.
Also tried to run it with 'harmony' flag:
nodejs --harmony myfile.js
...but I still get similar errors like:
for (const key of ['Schema', 'model']) {
^^
SyntaxError: Unexpected identifier
My dependencies in package.json are:
"dependencies": {
"express": "~4.17.1",
"mongoose": "~5.7.5"
}
And my Node.js version is v0.10.25
It seems I don't get to find the correct combination of packages or I am doing something else wrong. Any help would be appreciated.

Getting: Error from phantom after upgrading to Node 6

after upgrading a MEAN stack application from 0.12 to node 6 we get the following error in our process.error log:
package/node_modules/phantom/lib/index.js:23 return new Promise(resolve => resolve(new _phantom2.default(args, config)));
and the process.output shows:
[2016-12-17 16:11:31.785] [ERROR] console - 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 Module.require (module.js:364:17)
at require (module.js:380:17)
my version of phantomjs 1.9.8
this is the same problem reported previously:
phantomjs unexpected token
by some other user, but me being a relative newbie, I did not understand the answer or how it helps me fix the problem.
please direct me to a solution to this problem
This failure can be eliminated by using phantom versions prior to 2.0.0.
Notice that this is the phantom package and not the phantomjs package that is giving the error

nodejs SyntaxError: Unexpected token

I am using elasticsearch-exporter to export data from Elasticsearch.
The tool initially is a nodejs application.
When I try to use the following command node exporter.js to make the tool list all the available options, it crashes with the following exception
/home/me/storage/Elasticsearch-Exporter/log.js:54
exports.error = (...args) => !capture("ERROR", args) && console.log(timestamp() + util.format(...args).red);
^^^
SyntaxError: Unexpected token ...
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/anas/storage/Elasticsearch-Exporter/exporter.js:9:11)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
here is the line where the exception is thrown
exports.error = (...args) => !capture("ERROR", args) && console.log(timestamp() + util.format(...args).red);
I think the error is related to a different version of nodejs but I am not sure.
Here is the output of the node --version command v4.2.6
Here is the output of the npm --version command 3.10.6
Yes, indeed, ... is called the spread operator and is only available since Node.js 6
The elasticsearch-exporter project declares in its package.json file that it only works with node version > 6
So since you're running Node.js 4.2.6, you either need to upgrade your Node.js installation or fork the elasticsearch-exporter project and modify it to work with Node.js 4.2.6.

unable to install meanjs on linux mint

it has been one week now that i was trying to install meanjs stack and had no success.
Everytime I follow some tutorial and get errors. Now I end up with many issues, too much nodejs version, i dont know which one should be with what and why do karma not working ...etc
is there anyway to remove everything and install meanjs correctly and get it running successfully
+ WARNING: It is strongly recommended that you change sessionSecret config while running in production!
Please add `sessionSecret: process.env.SESSION_SECRET || 'super amazing secret'` to
`config/env/production.js` or `config/env/local.js`
/opt/mean/node_modules/connect-mongo/src/index.js:3
const Promise = require('bluebird');
^^^^^
error: uncaughtException: Use of const in strict mode. date=Mon Jul 18 2016 19:15:59 GMT+0400 (GST), pid=29602, uid=1000, gid=1000, cwd=/opt/mean, execPath=/usr/local/bin/node, version=v0.10.33, argv=[node, /opt/mean/server], rss=47960064, heapTotal=35267072, heapUsed=18951416, loadavg=[0.5966796875, 0.453125, 0.66748046875], uptime=4058.491096953
SyntaxError: Use of const in strict mode.
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 Module.require (module.js:364:17)
at require (module.js:380:17)
at Object. (/opt/mean/node_modules/connect-mongo/index.js:1:80)
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)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object. (/opt/mean/config/lib/express.js:12:16)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
below is my system config
Linux Mint 17.2 64-bit
NodeJS v0.10.33
npm 1.4.28
bower 1.7.9
mongodb v3.0.12
Can anyone please help me, I want to get started.
I haven't dug into it, but I'm guessing the version of node you have doesn't support the 'const' keyword. 0.10.x is a (fairly) old version of node; have you considered upgrading to one of the versions that has ES 6 native support?

Resources