Arrow function in Express exported class - Unexpected token

map arrow function
arrow functions in class properties
arrow function vs function
es6 function declaration

I have file with my class which I exported and use in express and Node. I'ld like to use arrow function and it's an example:

class MyClass {

    myFunc(arg) {
        console.log(arg);
    }

    myArrowFunc = (arg1, arg2) => {
        console.log(arg1);
        console.log(arg2);
    };
}


module.exports = {
    MyClass
}

Everything works fine before adding myArrowFunc. After that I have an error after run app:

myArrowFunc = (arg1, arg2) => {
            ^
SyntaxError: Unexpected token =

My Node version is 9.x so it should work... or I forgot about something.

You can't put an assignment in a Class like that.

Why not put it in the constructor? You initialize properties in it.

No babeljs needed for that.

class MyClass {
    constructor(){
        this.myArrowFunc = (arg1, arg2) => {
            console.log(arg1);
            console.log(arg2);
        };
    }
    myFunc(arg){
        console.log(arg);
    }
}

How To Enable ES6 Imports in Node.JS, The following example is for the express.js server. to the homepage app.get('/', function (req, res) { res.send('hello world') }) app.listen(3000, If you go an run it now as node server.js, you'll get an error SyntaxError: Unexpected token import. Import the rest of our application. module.exports = require('. alanaasmaa changed the title Unexpected token export (export default class Test {}) Unexpected token export (Arrow function in class) Mar 26, 2019

Starting with node versions v10.x.x, you can use node --harmony to run code with arrow notation for class methods. Harmony flag enables new ECMAScript 6 features in the language.

Unexpected token export (Arrow function in class) · Issue #5392 , With the latest update my class stopped working when using short functions. I assume something broke with core-js revert? export default class  VS Code integration breaks: the first = is underlined with this message:[eslint] Parsing error: unexpected token =. Linting is broken for entire file, no other errors are shown even though they exist, and autofix on save does not work. Everything works as expected for files without arrow functions both via CL and in VSCode. 👍

In order to use new Javascript (es6) in express app, you need to install babel to compile es6 to es5 code. The Babel compiler will allow you to use the new JavaScript features in Express app.

These dependencies are required to use the babel compiler in your project.

1) babel-cli: Compile files from the command line using babel

2) babel-preset-es2015: Babel preset for all es2015 plugins.

Run this command to install babel in your nodejs project using terminal

npm install -save-dev babel-cli babel-preset-es2015

Rest please follow this link https://www.codementor.io/iykyvic/writing-your-nodejs-apps-using-es6-6dh0edw2o

not recognizing arrow function () => {} resulting unexpected token , Syntax Error: Unexpected token ')' not understanding arrow function compilerOptions); module.exports = function (wallaby) { var typeScript(​compilerOptions) }, middleware: function (app, express) { var path  According to the es6 spec, class methods are free to use arrow function declarations. However, eslint throws a Parsing error: unexpected token = when it encounters these methods. Try the following: class App{ cl

You don't need Babel with Node, Of course, we can't forget our old source maps friend so we can export const double = (number) => number * 2; ^^^^^^ SyntaxError: Unexpected token export When using the type method on Node.js 12 and above, it has a nice side backticks (multi-line strings), fat arrows, and even the class keyword  Tell us about your environment ESLint Version: 4.15.0 Node Version: 8.9.4 npm Version: 5,6,0 What parser (default, Babel-ESLint, etc.) are you using? babel-eslint Please show your full configuration: Configuration module.exports = { exte

Trouble exporting and importing a class - JavaScript, Uncaught SyntaxError: Unexpected token export ( at laser.js:1). Uncaught SyntaxError: Unexpected identifier (at sketch.js:1) I've double  Now if we try to exacute the above, we'd get 'unexpected token "export"' appearing. The reason behind this is simple - Node.js uses the CommonJS Module syntax which assumes two other keywords: require() and module.exports .

TypeError: "x" is not a function, The JavaScript exception "is not a function" occurs when there was an attempt to call a Sometimes when making a class, you may have a property and a function with the same name. Import the exported module correctly SyntaxError: Unexpected token · SyntaxError: Using //@ to indicate sourceURL  The Express documentation describes a middlewares as: “[] functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.”

Comments
  • What's the point of a method that's an arrow function anyway? When you do that, it's not really a method of your object because this won't be the object instance. What problem are you actually trying to solve?
  • this is worked for me, but I don't get it why is not working without defining in constructor.
  • Pretty sure Node doesn't need babel for arrow functions to work nowadays. The issue is that the experimental feature of class fields or property initializers (or whatever they're called) hasn't made it into Node, but they are available through babel.
  • Trying to process the code in the question with Babel gives: SyntaxError: module.js: Unexpected token (7:16)
  • I'm not sure. Just in case if someone using the old version for eg: below 4.x.x ,hope it will help
  • Even in a recent version of Node this wouldn't work tho.