Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"SyntaxError: Unexpected token )" in Node.js

I keep getting a SyntaxError: Unexpected token )' error for the following code:

passport.use(
  'local-signup',
  new LocalStrategy({
      usernameField: 'email',
      passwordField: 'password',
      passReqToCallback: true, // pass back req to callback
    },
    (req, email, password, done) => {
      // ...
    },
  ),
);

It really only started after I put arrow functions in. I think I'm missing something syntax-wise. I'm using the airbnb style guide & linter btw. Running Node.js LTS. VS Code doesn't give any parsing errors either in the editor itself. The code works when transpiled to ES2015 via Babel. I'm still curious why it isn't working with the ES6 syntax.

like image 392
Rishi Speets Avatar asked Mar 08 '23 22:03

Rishi Speets


1 Answers

The problem is that in two places you're using a trailing comma in function syntax, i.e. a comma after the last argument of a function.

passport.use(
  'local-signup',
  new LocalStrategy({
      usernameField: 'email',
      passwordField: 'password',
      passReqToCallback: true, // pass back req to callback
    },
    (req, email, password, done) => {
      // ...
    },
//   ^
  ),
// ^
);

This syntax is part of ECMAScript 2017, and is not supported by Node.js before version 8.0.0, but can be transpiled using Babel.

like image 74
Michał Perłakowski Avatar answered Mar 19 '23 15:03

Michał Perłakowski