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
- Re-enable the views hook: set `hooks: { views: true }` in config/.sailsrc (remove any `views: false`).
- If the app is API-only, replace res.render() with res.json() or res.send() for responses.
- Ensure sails is loading hooks normally (no `loadHooks` whitelist in config that omits 'views').
- 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
- Do not disable the views hook in apps that render views.
- In API-only projects, standardize on res.json()/res.send() instead of res.render().
- Review loadHooks whitelists whenever removing hooks.
- Add a startup check that sails.hooks.views exists if you rely on view rendering.
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
- A leading `.` is not required in the config.views.extension
- E_VIEW_INFER
- i18n hook has disabled itself due to misconfiguration -- ret
- Invalid CORS settings for route ${route}
- `res.guessView()` is deprecated in Sails >= v1.0. If you wa
AI-assisted analysis of balderdashy/sails@7b76422cc2 (2026-09-01).
Data as JSON: /api/errors/493cc726f20e133f.
Report an issue: GitHub.