Options found in locals object in ejs

profile for Nikola at Stack Overflow, Q&A for professional and enthusiast programmers
I’m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%). In this category (stackoverflow) of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread here.

My question was:

I got the following error when running the project from GitHub: “options found in locals object. The option(s) is copied to the option object. This behavior is deprecated and will be removed in EJS 3”

I tried to update the ejs and express modules to the newest versions but the notice persists. I googled, ofc, and the only thread about it is this, but it doesn’t help.

Does anyone know more about this?

For reference, here is the whole important code:

app/views/index.ejs

<!DOCTYPE html>
<html>
<head>
    <title><%= title %></title>
</head>

<body>
    <h1><%= title %></h1>
    <img src="img/logo.jpg" alt="Hack Hands logo">
</body>
</html>

app/controllers/index.server.controller.js

exports.render = function(req, res) {
    res.render('index', {
        title: 'MEAN MVC'
    });
};

app/routes/index.server.route.js

module.exports = function(app) {
    var index = require('../controllers/index.server.controller');
    app.get('/', index.render);
};

app/config/express.js

var express = require('express');
module.exports = function() {
    var app = express();

    app.set('views', './app/views');
    app.set('view engine', 'ejs');

    require('../app/routes/index.server.routes.js')(app);

    app.use(express.static('./public'));

    return app;
};

server.js

var port = 1337;
var express = require('./config/express');
var app = express();
app.listen(port);
module.exports = app;
console.log('Server running at http://localhost:' + port);

The answer, by Timothy Gu, was:

tl;dr: Upgrade to the latest version of EJS. It removes all warnings about options and locals.

whoami

I’m a collaborator (or the collaborator in @micnic’s comment above) in EJS v2. I only started maintaining EJS after version 2.0.3 (or something like that) was released, so I don’t know a lot about how the API changes took place.

History

EJS v2’s renderFile function, used by Express.js, now has the signature

function(path[, options[, locals]], cb)

But for compatibility with and Express.js, which calls all functions as

function(path, locals, cb)

with options mixed into the locals object, EJS automatically picks out the locals with option-y names and treat them as options.

But because the Express.js signature is also the function signature of EJS v1, we also print a warning if any option in locals is copied to options, urging developers to use the new signature with locals and options separated (it was actually me who added the warning).

However, Express.js users do not have a choice in terms of calling convention, so the warning is always present in Express.js.

Some users did complain: #34 #36.

At first, @mde (who is the main maintainer of EJS) pushed a fix, which correctly disables warnings on Express.js, and Express.js only.

But then, the person in #36 still complained, as he was using filename as the name of a local, and when the optiony local is copied to options a warning is printed.

At last, @mde was like “f*** this shit” and removed all the deprecation warnings, including an uncontroversial and legitimate one, and released version 2.2.4 (the legitimate warning was restored by me after the release).

Future

@dougwilson (an Express.js maintainer) said he was interested in a separation of options and locals in Express.js v5, just like in EJS v2. I did volunteer to make that change, but then I got busy so yeah.

Written by Nikola Brežnjak