Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting SyntaxError: Unexpected token '?' when using 'express-rate-limit'

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

like image 488
Leo Avatar asked Oct 19 '25 10:10

Leo


1 Answers

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.

like image 59
code Avatar answered Oct 21 '25 23:10

code