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

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.

Related

SyntaxError: Unexpected token ... in serialport in node_modules

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

Run native Nodejs in AS400 / iSeries

I'm trying to run Node.js on AS/400 server in order to use Node.js toolkit for IBM i.
I have installed Node v6.14.4 and NPM 3.10.10 and I'm following this documentation to test node.js: https://developer.ibm.com/articles/i-native-js-app-ibmi-with-nodejs/#access-ibm-i-native-objects
I created the sample javascript file and when I run (from PASE terminal), I get this error:
node /home/njs/sample.js
/sample.js:1
(function (exports, require, module, __filename, __dirname) { #����ï¿
½ï¿½ï¿½K���M È ï¿½ï¿½#����� ]^
SyntaxError: Invalid or unexpected token
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.runMain (module.js:611:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:160:9)
I supposed there is a problem with npm and I tried with a very simple js file (an only one console.log) and I get the same error.
I have solved my issue. It was because an enconding problem.
I was creating javascript files with EDTF on AS/400 environment and that was a problem. I created a new file with notepad and upload by Navigator for i and works properly.

Can't run node project after formatting my linux partition

As the title says, I formatted my linux partition where I used as web development environment. Mainly I used MongoDB and NodeJS and both of them are installed correctly. But when I try to execute my main.js I get an error log. It must be something extremely silly that I'm not reminded of. Any help is appreciated. Error log:
SyntaxError: Invalid or unexpected token
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Object.runInThisContext (vm.js:298:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
Note that helpers.js is a node internal module.
Please download nodeJS command prompt for Linux or download by command
pacman -S nodejs npm
For More help : Visit the link
Node JS Installation Guide

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.

Resources