expressjs / express

Fast, unopinionated, minimalist web framework for node.
https://expressjs.com
MIT License
64.35k stars 14.63k forks source link

WARNING: Detected use of sync API at fs.statSync (fs.js:892:18) #2960

Closed DJviolin closed 8 years ago

DJviolin commented 8 years ago

I enable the --trace-sync-io flag for Node to trace synchronous calls. It gives error for every res.render in my app.

Maybe not connected for this error, but my express-handlebars templates only be refreshed if I restart the server.

Here is the router:

'use strict';

var express = require('express'),
    router  = express.Router();

/* GET info page. */
router.get('/', function(req, res, next) {
  res.render('contact', {
    titleShown: true,
    title: 'Contact',
    description: 'Get in touch',
    keywords: 'info,wedding,photography,film,lantos,istvan'
  });
});

module.exports = router;

This is the console log:

$ SET DEBUG=lantosistvan-portfolio:*,i18n:* & npm start

> lantosistvan-portfolio@0.0.0 start C:\www\node\lantosistvan
> node --trace-deprecation --trace-sync-io ./bin/www

  i18n:debug will use C:\www\node\lantosistvan\locales\hu.json +0ms
  i18n:debug read C:\www\node\lantosistvan\locales\hu.json for locale: hu +5ms
  i18n:debug will use C:\www\node\lantosistvan\locales\en.json +3ms
  i18n:debug read C:\www\node\lantosistvan\locales\en.json for locale: en +1ms
  lantosistvan-portfolio:server Listening on port 3000 +39ms
WARNING: Detected use of sync API
    at fs.statSync (fs.js:892:18)
    at tryStat (C:\www\node\lantosistvan\node_modules\express\lib\view.js:169:15)
    at resolve (C:\www\node\lantosistvan\node_modules\express\lib\view.js:142:14)
    at lookup (C:\www\node\lantosistvan\node_modules\express\lib\view.js:110:17)
    at View (C:\www\node\lantosistvan\node_modules\express\lib\view.js:85:20)
    at render (C:\www\node\lantosistvan\node_modules\express\lib\application.js:569:12)
    at render (C:\www\node\lantosistvan\node_modules\express\lib\response.js:961:7)
    at C:\www\node\lantosistvan\routes\contact.js:8:7
    at handle (C:\www\node\lantosistvan\node_modules\express\lib\router\layer.js:95:5)
GET /contact 304 124.912 ms - -
DavidTPate commented 8 years ago

Duplicate of #2959

dougwilson commented 8 years ago

Yes, there are sync parts of the res.render API (which sucks), but it will be addressed in Express 5.0, as we cannot address it without breaking the view engine compatibility.

Starting your application with NODE_ENV=production or setting the cache to true for rendering will cause file system activities only once per view at startup, which makes this a non-issue while the application is fully running in production, since no sync file systems are called since the views are cached.

Please also see the comments in the duplication issue @DavidTPate pointed out :)

dougwilson commented 8 years ago

You can also find all this information, from using the trace sync flag to how to properly setup your Express application in production on the website: http://expressjs.com/en/advanced/best-practice-performance.html

DJviolin commented 8 years ago

Starting your application with NODE_ENV=production or setting the cache to true for rendering will cause file system activities only once per view at startup

Wow, this is really useful to know! Probably I needed to spend a lot of time to figure out why my handlebars templates only refreshed on server reload, didn't realising that few days ago I added production environment variable to my OS! :)

But is there a way to set express-handlebars templating engine for always refresh from the disk on refresh? After all, this is don't needed when my static site goes to production (blog posts already read from a json file which is cached on every browser reload), but it will be good to know how I can turn on the viewing template reload on every browser load.