Node.js project naming conventions for files & folders

What are the naming conventions for files and folders in a large Node.js project?

Should I capitalize, camelCase, or under-score?

Ie. is this considered valid?

project-name
    app
        controllers
            someThings.js
            users.js
        models
                someThing.js
                user.js
        views
            some-things
                index.jade
            users
                logIn.jade
                signUp.jade
    ...

After some years with node I can say that there are no conventions for the directory/file structure. However most (professional) express applications use a setup like:

/
  /bin - scripts, helpers, binaries
  /lib - your application
  /config - your configuration
  /public - your public files
  /test - your tests

An example which uses this setup is nodejs-starter.

I personally changed this setup to:

/
  /etc - contains configuration
  /app - front-end javascript files
    /config - loads config
    /models - loads models
  /bin - helper scripts
  /lib - back-end express files
    /config - loads config to app.settings
    /models - loads mongoose models
    /routes - sets up app.get('..')...
  /srv - contains public files
  /usr - contains templates
  /test - contains test files

In my opinion this matches better the unix-style directory structure (whereas the above mixes this up a bit).

I also like this pattern to separate files:

lib/index.js

var http = require('http');
var express = require('express');

var app = express();

app.server = http.createServer(app);

require('./config')(app);

require('./models')(app);

require('./routes')(app);

app.server.listen(app.settings.port);

module.exports = app;

lib/static/index.js

var express = require('express');

module.exports = function(app) {

  app.use(express.static(app.settings.static.path));

};

This allows decoupling neatly all source code with out having to bother dependencies. A real good solution for fighting nasty javascript. A real world example is nearby which uses this setup.

There are no conventions. There are some logical structure.

The only one thing that I can say: Never use camelCase file and directory names. Why? It works but on Mac and Windows there are no different between someAction and some action. I met this problem, and not once. I require'd a file like this:

var isHidden = require('./lib/isHidden');

But sadly I created a file with full of lowercase: lib/ishidden.js. It worked for me on mac. It worked fine on mac of my co-worker. Tests run without errors. After deploy we got a huge error:

Error: Cannot find module './lib/isHidden'

Oh yeah. It's a linux box. So camelCase directory structure could be dangerous. It's enough for a colleague who is developing on Windows or Mac.

So use underscore (_) or dash (-) separator if you need.

For this and other question about convention for Node.js, Felix Geisendörfer has published a great style guide for this purpose.

Node.js doesn't enforce any file naming conventions (except index.js). And the Javascript language in general doesn't either. You can find dozens of threads here which suggest camelCase, hyphens and underscores, any of which work perfectly well. So its up to you. Choose one and stick with it.

Most people use camelCase in JS. If you want to open-source anything, I suggest you to use this one :-)

I already gave a +1 to @bodokaiser. The following is only my comment.

The official node webpage has some code in it. I always assume they have followed that convention and would be good if we continue that style. As so many programmers already used to that.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

For packages - in turn the directory names I believe, they have used small letters. var http = require('http');. (It might go camelCase if it contains multiple words)

In http.createServer(function (req, res) {..., it is clear that they have used camelCase for method names / aka functions.

In console.log('Server running at http://127.0.0.1.com:1337/');, we can observe that all globals like console and process, etc are small letters. Observe that in process global class, you find process.version, process.pid, process.platform, process.installPrefix etc are small, camelcases. but when it comes to methods/functions, like process.getgid(), process.getcwd() etc, they did not use camelcase for methods. So here, I believe that node has some coding practices only to differ from globals and non globals.

Also observe, there are some classes like EventEmitter. they followed java style of naming classes. Here I follow one point. all globals are small case (not even camelcase), and all packaged classes are java style - Capitalized. which means if you are defining a class of your own like function MyFunction(){...}, you normally wish to create it capitalized.