4

I am trying make use of 'express-rate-limit' and for some reason when running the server I am getting SyntaxError: Unexpected token '?' even though I am pretty sure my script does not have any syntax error.

Here is de code:

rateLimiter.js

const rateLimit = require('express-rate-limit');

const rateLimiter = (limit, timeframeInMinutes) => {

    return rateLimit({
        max: limit,
        windowMs: timeframeInMinutes * 60 * 1000,
    
        message: {
            error: {
                status: 429,
                message: 'TOO_MANY_REQUESTS',
                expiry: timeframeInMinutes,
            },
        },
    
    });
};

module.exports = rateLimiter;

auth.js

const express = require('express');
const authController = require('../controllers/auth');
const rateLimiter = require('../helpers/rateLimiter');

// Router initialisation
const router = express.Router();

// Routes
router.get('/test', rateLimiter(1, 10), authController.test);

module.exports = router;

Here is a screenshot of the error:

enter image description here

Diego
  • 386
  • 4
  • 19
  • 1
    Does this answer your question? [SyntaxError: Unexpected token '?'](https://stackoverflow.com/questions/69329814/syntaxerror-unexpected-token) – code Jan 28 '22 at 18:04

3 Answers3

4

You are trying to use nullish coalescing (??) on an unsuported version of Node. Nullish coalescing is supported from Node v14 and up.
For now the simple alternative is ||, unless you upgrade your version.

code
  • 5,690
  • 4
  • 17
  • 39
3

From the documentation:

This package requires you to use Node 14 or above.

The ?? operator throwing an error indicates that you're using an older version.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

You don't indicate what version of node you are using. The nullish coalescing operator was not added until version 14.

kevintechie
  • 1,441
  • 1
  • 13
  • 15