mape / connect-assetmanager

Middleware for Connect (node.js) for handling your static assets.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error: watch ENOENT on express server

ellmo opened this issue · comments

This is my app.js handlerd by express server:

/**
 * Module dependencies.
 */

var express = require('express');
var http = require('http');
var path = require('path');
var assetManager = require('connect-assetmanager');

var assetManagerGroups = {
  js: {
    route: /\/static\/js\/[0-9]+\/.*\.js/,
    path: './public/javascripts/lib',
    dataType: 'javascript',
    files: [ 'jquery.js' ]
  }
};

var assetsManagerMiddleware = assetManager(assetManagerGroups);

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
// app.use(assetsManagerMiddleware)
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/static/js/', express.static(__dirname + '/public/javascripts'));
app.get('/static/css/', express.static(__dirname + '/public/stylesheets'));
app.get('/static/img/', express.static(__dirname + '/public/images'));
app.get('/*', function(req, res){
  res.render('index', { title: 'Express' });
});

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

When the var assetsManagerMiddleware = assetManager(assetManagerGroups); line is uncommented, the server throws:

ellmo@ellmo~/node/ss2(master)$ node app.js
Express server listening on port 3000

/Users/ellmo/node/ss2/node_modules/connect-assetmanager/node_modules/step/lib/step.js:39
        throw arguments[0];
                       ^
Error: watch ENOENT
    at errnoException (fs.js:1019:11)
    at FSWatcher.start (fs.js:1051:11)
    at Object.fs.watch (fs.js:1076:11)
    at /Users/ellmo/node/ss2/node_modules/connect-assetmanager/lib/assetmanager.js:114:9
    at Array.forEach (native)
    at /Users/ellmo/node/ss2/node_modules/connect-assetmanager/lib/assetmanager.js:105:17
    at /Users/ellmo/node/ss2/node_modules/connect-assetmanager/lib/assetmanager.js:32:6
    at Array.forEach (native)
    at Object.settings.forEach (/Users/ellmo/node/ss2/node_modules/connect-assetmanager/lib/assetmanager.js:30:22)
    at Function.<anonymous> (/Users/ellmo/node/ss2/node_modules/connect-assetmanager/lib/assetmanager.js:103:12)

As you can see it does that after actually starting the server. I wish there was something to read and debug, but there isn't. I'm at a complete loss.

Well, this is awkard. I just had the same issue and fixed it... but your issue is several months old. I'll still answer anyway. That error is thrown by fs.watch when it's trying to open a file that doesn't exist. The problem here, is the way connect-assetmanager is concatenating the path and files strings. To workaround this problem, make sure your path property has a trailing slash:

var assetManagerGroups = {
  js: {
    route: /\/static\/js\/[0-9]+\/.*\.js/,
    path: './public/javascripts/lib/',
    dataType: 'javascript',
    files: [ 'jquery.js' ]
  }
};

This is something that should be fixed in connect-assetmanager. The problematic line is 114 of assetmanager.js:

fs.watch(group.path + file, function (event, file) {
    if (event === 'change') {
        self.generateCache(groupName);
    }
});

The group.path + file part should read path.join(group.path, file). The method path.join handles having or not having a trailing slash in either path or file, as well as using the correct backward slash (\) in Windows or forward slash (/) everywhere else.