SyntaxError: Unexpected token ... in serialport in node_modules - node.js

I hav been trying to run the vue-electron app. But the following error comes.
App threw an error during load
C:\newFolder02\pos4-desktop\node_modules\#serialport\stream\lib\index.js:103
const settings = { ...defaultSettings, ...options }
^^^
SyntaxError: Unexpected token ...
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\newFolder02\pos4-desktop\node_modules\serialport\lib\index.js:1:175)
The error comes from the following line.
const settings = { ...defaultSettings, ...options }
I use following tools
node 8.16.0
npm 6.4.1
electron 1.8.2
It doesn't work in Windows. But it works in Mac OS.
Any idea..?

I have found a solution. You can convert the ES6 code to original JavaScript code with Babel(https://babeljs.io/en/repl).
I converted the ES6 code to vanilla JavaScript through Babel. I ticked ENV PRESET in left sidebar. I set Electron to "1.8" and Node to "8.16". It worked well.
Babel Screenshot

Related

Node js script not starting up, async * Unexpected Token

I am trying to run my node script with node . on Ubuntu 18.04
It worked successfully on previous machine. This is a new setup and running into this error.
/var/www/html/node_bot/node_modules/discord.js/src/structures/interfaces/Collector.js:203
async *[Symbol.asyncIterator]() {
^
SyntaxError: Unexpected token *
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
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> (/var/www/html/node_bot/node_modules/discord.js/src/structures/MessageCollector.js:3:19)
My node version is v8.10.0 on all accounts including root.
I have tried chowning the files to root and running from root, same error.
Am I missing a package or something? Thanks
Use a newer node version.
From library documentation
https://github.com/discordjs/discord.js
Installation
Node.js 10.2.0 or newer is required.
Updated to node v10.18.1 and it works.

How to fix the following error creating a node js class?

Hello I started creating a node.js class as follows:
'use strict';
let {google} = require('googleapis')
class Calendar {
calendarId : string;
However, when I test my code I got:
src/utilities/calendar.ts:6
calendarId : string;
^
SyntaxError: Unexpected token :
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
So I don't understand why I am getting that, I would like to appreciate suggestions to overcome the issue. My Node version is 8.16.0 and I am also using typescript
You are writing a typescript file, not a javascript file.
Node cannot automatically run typescript files. You need to:
install typecript
compile the file
run the output with node.js
Alternatively you could use a node like ts-node to execute your typescript right from the command-line.

NodeJS Error In Constructor Language Only Present On Ubuntu

I am trying to get a particular npm package to run on Ubuntu 16.04. I am currently running the most up-to-date versions of nodejs and npm, as well as the package (turndown).
Here is the error that I get:
root#ubuntu-s-1vcpu-1gb-nyc1-01:/usr/server# nodejs index.js
/usr/server/node_modules/jsdom/lib/api.js:10
const { URL } = require("whatwg-url");
^
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 createHTMLParser (/usr/server/node_modules/turndown/lib/turndown.cjs.js:529:17)
at Object.<anonymous> (/usr/server/node_modules/turndown/lib/turndown.cjs.js:537:60)
at Module._compile (module.js:410:26)
The error states it occurs within the "turndown" package and is due to the language in the constructor. What is confusing to me is that this runs on Fedora 27 without error.
What could be causing the difference in behavior?
The answer to this question was to update my version of NodeJS. I ran into another error now, but it is package related.

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.

Import keyword is not recognized by gulp throwing SyntaxError: Unexpected reserved word

Failed to load external module babel-core/register
Failed to load external module babel/register
somelocation\gulpfile.babel.js:27
import fs from 'fs';
^^^^^^
SyntaxError: Unexpected reserved word
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Liftoff.handleArguments (\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:116:3)
at Liftoff.<anonymous> (\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\index.js:192:16)
at module.exports (\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\node_modules\flagged-respawn\index.js:17:3)
I am getting this error while trying to run gulp after npm as described here: deploy-appengine.md. How to fix it?
EDIT: as Luke Bennet points out correctly in a comment to this answer, it was indeed JavaScript - the ES6 "import" syntax. The issue is that this won't work out of the box in Node as yet.
You seem to be using the python module import syntax in NodeJS. You should use the NodeJS module require syntax.

Resources