balderdashy/sails · error

Cannot call res.render() - `req._sails.renderView` was not a

Error message

Cannot call res.render() - `req._sails.renderView` was not attached (perhaps `views` hook is not enabled?)

What it means

res.render() requires that the Sails views hook has attached `req._sails.renderView`. The shim in lib/router/res.js throws when the request reached the response object without the views hook's decoration, which happens when the `views` hook is disabled (e.g. `sails.config.hooks.views === false`) or a project was generated without views support.

Source

Thrown at lib/router/res.js:324

      res.set('content-type', 'application/json');
    }

    return res.status(res.statusCode || 200).send(data);
  };

  // res.render()
  res.render = res.render || function _renderShim (relativeViewPath, locals, cb) {
    if (_.isFunction(locals)) {
      cb = locals;
      locals = {};
    }

    try {
      if (!req._sails) {
        throw new Error('Cannot call res.render() - `req._sails` was not attached');
      }
      if (!req._sails.renderView) {
        throw new Error('Cannot call res.render() - `req._sails.renderView` was not attached (perhaps `views` hook is not enabled?)');
      }

      res.set('content-type', 'text/html');

      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      // TODO:
      // Instead of this shim, turn `sails.renderView` into something like
      // `sails.hooks.views.render()`, and then call it.
      throw flaverr({statusCode: 501}, new Error('Not implemented in core yet'));
      //
      // Instead, do something like the following:
      // ```
      // var html;
      // // ...
      // if (cb) {
      //   return cb(undefined, html);
      // }
      // else {

View on GitHub (pinned to 7b76422cc2)

Solutions

  1. Re-enable the views hook: set `hooks: { views: true }` in config/.sailsrc (remove any `views: false`).
  2. If the app is API-only, replace res.render() with res.json() or res.send() for responses.
  3. Ensure sails is loading hooks normally (no `loadHooks` whitelist in config that omits 'views').
  4. Install/restore the views hook dependencies (e.g. missing template engine config) if the hook fails to load silently.

Example fix

// before (.sailsrc)
{ "hooks": { "views": false } }
res.render('pages/home');
// after
{ "hooks": { "views": true } }
res.render('pages/home');
// or, in an API-only app:
res.json({ page: 'home' });
Defensive patterns

Strategy: validation

Validate before calling

if (!sails.config.hooks.views) { throw new Error('res.render() unavailable: views hook is disabled'); }

Type guard

function canRender(res) { return typeof res.render === 'function' && req._sails && typeof req._sails.renderView === 'function'; }

Try / catch

try { res.render('pages/home'); } catch (e) { if (/views.*hook is not enabled/.test(e.message)) { res.json({ fallback: true }); } else { throw e; } }

Prevention

When it happens

Trigger: Calling res.render() (or a controller/view rendering path) while `hooks.views` is disabled in .sailsrc or config/views.js, or when a custom hook pipeline replaced/omitted the views hook.

Common situations: Disabling the views hook in an API-only Sails app and later adding a res.render() call; copying code from a web project into an api-only project; using a bare Express-style middleware that assumes res.render exists with template support.

Related errors


AI-assisted analysis of balderdashy/sails@7b76422cc2 (2026-09-01). Data as JSON: /api/errors/493cc726f20e133f. Report an issue: GitHub.