{"record":{"id":"493cc726f20e133f","repo":"balderdashy/sails","slug":"cannot-call-res-render-req-sails-renderview","errorCode":null,"errorMessage":"Cannot call res.render() - `req._sails.renderView` was not attached (perhaps `views` hook is not enabled?)","messagePattern":"Cannot call res\\.render\\(\\) - `req\\._sails\\.renderView` was not attached \\(perhaps `views` hook is not enabled\\?\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/router/res.js","lineNumber":324,"sourceCode":"      res.set('content-type', 'application/json');\n    }\n\n    return res.status(res.statusCode || 200).send(data);\n  };\n\n  // res.render()\n  res.render = res.render || function _renderShim (relativeViewPath, locals, cb) {\n    if (_.isFunction(locals)) {\n      cb = locals;\n      locals = {};\n    }\n\n    try {\n      if (!req._sails) {\n        throw new Error('Cannot call res.render() - `req._sails` was not attached');\n      }\n      if (!req._sails.renderView) {\n        throw new Error('Cannot call res.render() - `req._sails.renderView` was not attached (perhaps `views` hook is not enabled?)');\n      }\n\n      res.set('content-type', 'text/html');\n\n      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n      // TODO:\n      // Instead of this shim, turn `sails.renderView` into something like\n      // `sails.hooks.views.render()`, and then call it.\n      throw flaverr({statusCode: 501}, new Error('Not implemented in core yet'));\n      //\n      // Instead, do something like the following:\n      // ```\n      // var html;\n      // // ...\n      // if (cb) {\n      //   return cb(undefined, html);\n      // }\n      // else {","sourceCodeStart":306,"sourceCodeEnd":342,"githubUrl":"https://github.com/balderdashy/sails/blob/7b76422cc27823df033572bdda5c4910a68b697f/lib/router/res.js#L306-L342","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (.sailsrc)\n{ \"hooks\": { \"views\": false } }\nres.render('pages/home');\n// after\n{ \"hooks\": { \"views\": true } }\nres.render('pages/home');\n// or, in an API-only app:\nres.json({ page: 'home' });","handlingStrategy":"validation","validationCode":"if (!sails.config.hooks.views) { throw new Error('res.render() unavailable: views hook is disabled'); }","typeGuard":"function canRender(res) { return typeof res.render === 'function' && req._sails && typeof req._sails.renderView === 'function'; }","tryCatchPattern":"try { res.render('pages/home'); } catch (e) { if (/views.*hook is not enabled/.test(e.message)) { res.json({ fallback: true }); } else { throw e; } }","preventionTips":["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."],"tags":["sails","views","render","config"],"backgroundTag":"views-hook-disabled","analyzedSha":"7b76422cc27823df033572bdda5c4910a68b697f","analyzedAt":"2026-09-01T04:01:57.104Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}